我正在尝试对需要HTTP标头(REST参数)的API进行AJAX调用。目前没有任何数据返回。我认为最困难的是理解setRequestHeader,甚至不确定是否有必要。在此示例中,msdn带有2个字符串参数:oReq.setRequestHeader("Content-Type", "text/xml")但是授权标头又在哪里呢?请帮忙

目前我有这个:

var baseURL = "https://api.azuga.com/azuga-ws/v1/live/location.json";

var header = "Authorization: Basic 0JRGDJW587832"; //Made up number

$.ajax({
      url: baseURL,
      dataType: 'json',
      beforeSend: function(xhr){xhr.setRequestHeader(header);},
      success: function(data){
            console.log(data);

 }
});

最佳答案

您是否尝试过:

var baseURL = "https://api.azuga.com/azuga-ws/v1/live/location.json";



$.ajax({
      url: baseURL,
      dataType: 'json',
      headers: { 'Authorization': 'Basic 0JRGDJW587832' },
      beforeSend: function(xhr){xhr.setRequestHeader(header);},
      success: function(data){
            console.log(data);

 }
});




还请注意,您的header是字符串,因此setRequestHeader没有采用两个参数!

09-26 06:44