ic-ajax icon indicating copy to clipboard operation
ic-ajax copied to clipboard

How do I set headers with ajax() method?

Open millisami opened this issue 10 years ago • 2 comments

I want to attach request headers with ajax() method. I tried with the following syntax, but its not working:

   var headers = {'Authorization': this.session.content.token}   
   return ajax({
            type: 'GET',
            url: "http://localhost:3000/places/" + location.latitude + "/" + location.longitude + "/2000.json",
            beforeSend: function (request) {
              request.setRequestHeader(headers);
            }
        });

But get the error in setRequestHeader:

 Error while processing route: index undefined is not a function TypeError: undefined is not a function
    at Object.jQuery.extend.ajax.jqXHR.setRequestHeader (http://localhost:4200/assets/vendor.js:9106:23)
    at Object.__exports__.default.Ember.Route.extend.model.get.getLocation.then.ajax.beforeSend (cabbyguide/routes/index.js:24:27)
    at Function.jQuery.extend.ajax (http://localhost:4200/assets/vendor.js:9259:39)
    at http://localhost:4200/assets/vendor.js:61982:17
    at initializePromise (http://localhost:4200/assets/vendor.js:59039:9)
    at new Promise (http://localhost:4200/assets/vendor.js:60635:9)
    at makePromise (http://localhost:4200/assets/vendor.js:61971:14)
    at raw (http://localhost:4200/assets/vendor.js:61930:14)
    at request (http://localhost:4200/assets/vendor.js:61917:18)
    at eval (cabbyguide/routes/index.js:20:18)

How do I set the headers?

millisami avatar Feb 24 '15 06:02 millisami

~~ajax is just an object. To do a request, use either of the following methods: ajax.request() or ajax.raw(). The ic-ajax readme is not very explicit, but i got this info from there.~~

Also, you're adding headers to your request in a funny way. As ic-ajax is just a wrapper over jQuery, you do it as you would normally do with jQuery:

import icajax from 'ic-ajax';
icajax({
  url: 'http://i.imgur.com/93IWfhg.jpg',
  headers: {'Authorization': this.session.content.token}
});

Reference: http://api.jquery.com/jquery.ajax/ (Ctrl-F for headers (default: {})).

lolmaus avatar Feb 24 '15 06:02 lolmaus

$.ajax({ url: "https://platform.podium.com/api/v2/review_invitations", dataType: 'json', contentType: "application/json", data: jsonData, credentials: 'same-origin', mode:'no-cors', headers: { 'Access-Control-Allow-Origin': '*' }, beforeSend: function (xhr, settings) { xhr.setRequestHeader('Authorization', 'Bearer xxxx'); }, //set tokenString before send success: function (data, status) { debugger; alert("Sucess"); }, error: function (xhr, status, error) { debugger; alert("Error! :" + xhr.status); alert(status); alert(error); }

        });

I am using Bearer token not working.any one can help

MuraliKrishna1221 avatar Aug 23 '18 11:08 MuraliKrishna1221