使用API​​密钥调用jQuery ajax

时间:2022-11-29 12:20:21

I have coded an AJAX call, but I don't know how to use the API key that the API has provided. Where do I put it in? Please provide with code example.

我编写了一个AJAX调用,但我不知道如何使用API​​提供的API密钥。我把它放在哪里?请提供代码示例。

The API key should be sent in the header:

API密钥应在标头中发送:

request.setRequestHeader('Authorization', 'key="mykey"');
function myFunc() {
  $.ajax({
    url: "http://example.php",
    dataType: "JSON",
  }).done(function(data) {
    // Done
  }).fail(function(data) {
    // Failed
  });
}

1 个解决方案

#1


1  

To add a header to the AJAX request you can set the headers property. Try this:

要向AJAX请求添加标头,您可以设置headers属性。试试这个:

function myFunc() {
  $.ajax({
    url: "http://example.php",
    dataType: "JSON",    
    headers: {
      Authorization: 'key=[YOUR_API_KEY_HERE...]'
    }
  }).done(function(data) {
    // Done
  }).fail(function(data) {
    // Failed
  });
}

#1


1  

To add a header to the AJAX request you can set the headers property. Try this:

要向AJAX请求添加标头,您可以设置headers属性。试试这个:

function myFunc() {
  $.ajax({
    url: "http://example.php",
    dataType: "JSON",    
    headers: {
      Authorization: 'key=[YOUR_API_KEY_HERE...]'
    }
  }).done(function(data) {
    // Done
  }).fail(function(data) {
    // Failed
  });
}