简体   繁体   中英

a.toLowerCase when add CORS to the ajax header

I have a Django app online and an API which requires to add special token into header.

When I add it to the $.ajaxSetup it still requires from me CORS Access-Control-Allow-Origin param in header.

When I add it, I'm getting an error: TypeError: a.toLowerCase is not a function

The weird part is when I use the same code in my HttpRequester firefox plugin because I'm getting the proper response with expected data.

The code:

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader({
          'CORS': 'Access-Control-Allow-Origin',
          'token': 'tokenStringHere'
        });
    }
});

$.ajax({
  url: 'myPathToOnlineAppWithApiString',
  type: 'GET',
  contentType: 'application/json',
  headers: {
  }
}).success(function(result){
  console.log('yes!');
}).error(function(error){
  console.log('no!');
});

setRequestHeader accepts two strings, the header name and its value, not an object. See The jqXHR section of the $.ajax entry of the jQuery docs for more information.

You should replace the object in the parameter with two calls to set those headers individually:

$.ajaxSetup({
     beforeSend: function(xhr) {
        xhr.setRequestHeader('CORS', 'Access-Control-Allow-Origin');
        xhr.setRequestHeader('token', 'tokenStringHere');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM