How to retrive value from http response header using datajs (ODATA))

Apr 25, 2012 at 10:23 AM

Hi everyone,

 

I have problems with CSRF token when I request a post request to the server.

I have read somewhere that suggest to fetch it in the request with

headers: {"X-CSRF-Token":"Fetch"}

and then later get the token from the response.

 

However, I do not know how to read the response header using the datajs.

Anyone who knows this? I really appriciate if someone could assist me in this matter.

 

Thank you.

Apr 26, 2012 at 11:39 PM

Hi Bluryone,

   You can receive a second argument in the success handler that you pass to the OData.read and OData.request functions.  This argument is the response object and you can access the headers as follows:

OData.read("http://myHost/myService.svc",
  function (data, request) {
      var myHeader = request.headers["my-header"];
      // do something interesting with the data.
  }, 
  function (err) {
      var request = err.request; // the request that was sent.
      var response = err.response; // the response that was received.
      // handle the error
  });

Keep in mind that if you are using JSONP you will get a response object entirely fabricated by the library.

Regards,

Alex Trigo.