将HTTP标头添加到AJAX获取请求

时间:2015-06-10 23:40:25

标签: javascript ajax rest http-headers

我正在尝试对需要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);

 }
});

1 个答案:

答案 0 :(得分:1)

你有没有尝试过:

<dependency>
  <groupId>it.unimi.di</groupId>
  <artifactId>mg4j-big</artifactId>
  <version>${mg4j-big.version}</version>
  <exclusions>
    <exclusion>
      <groupId>jdk.tools</groupId>
      <artifactId>jdk.tools</artifactId>
    </exclusion>
  </exclusions>
</dependency>

另请注意,您的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没有使用两个参数!