Skip to Content

How to read X-CSRF-TOKEN from JSON model in UI5?

Hi,

I have developed one application for desktop and mobile .In desktop application I used oData model to read CSRF token. It's working fine and I can create records in back end.

But in my mobile application I am using JSON model due to some problems .So, if anyone knows please tell me how to read X-CSRF-TOKEN from JSON model.

Thanks&Regards

Sridevi.G

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Oct 10, 2013 at 11:05 AM

    Hi

    try this

    oModel.setHeaders(

     

                                                                {

                                                                           "X-Requested-With": "XMLHttpRequest",

                                   "Content-Type": "application/atom+xml",

                                   "DataServiceVersion": "2.0",      

                                   "X-CSRF-Token":"Fetch" 

     

                                                                }

     

                                            );

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 10, 2013 at 11:35 AM

    Hi,

    Thanks for your quick reply.

    Could you please explain me little bit more.

    Actually when I am using oData model, I am  getting data by using get and post the data using post request like below.

    OData.request({

    requestUri : "http://xxxx:8000/sap/opu/odata/EAE/SERVICE_ENTRYSHEET_SRV/PurchaseOrderItems(PoDocnum='"+PoDocnum+

    "',PoItem='" + PoItem   + "')?$expand=ServiceSet",

    method : "GET",

    headers : {

    "X-Requested-With" : "XMLHttpRequest",

    "Content-Type" : "application/atom+xml ",

    "DataServiceVersion" : "2.0",

    "X-CSRF-Token" : "Fetch"

    } },

    function(data, response) {

    console.log(response);


    var header_xcsrf_token = response.headers['x-csrf-token'];


    //posting data

    OData.request( {

    requestUri : "http://xxxx:8000/sap/opu/odata/EAE/SERVICE_ENTRYSHEET_SRV/PurchaseOrderItems",

    method : "POST",

    headers : {

    "X-Requested-With" : "XMLHttpRequest",

    "Content-Type" : "application/json",

    "DataServiceVersion" : "2.0",

    "Accept" : "application/atom+xml,application/atomsvc+xml,application/xml",

    "x-csrf-token" : header_xcsrf_token

    },

    data : totaldata //sending data as an object to the backend

    },

    function(data, response) {

    sap.ui.commons.MessageBox.alert("successfully Created");

    },

    function(err) {

    // sap.ui.commons.MessageBox.alert(result);

    }, function(err) {

    var request = err.request;

    var response = err.response;

    });

    How can I do the same thing when I am using Json model ?

    Thanks&Regards

    Sridevi.G

    Add comment
    10|10000 characters needed characters exceeded

    • I already checked all those things.

      The data which I am sending to the back end for oData model application and JSON model application both are same .

      Still I am getting same error.

      I am guessing that the problem with JSON model.

      I also tried with ajax call (GET and POST methods). But I am getting CSRF token validation failed error message.

      how can i set the CSRF token through the ajax call?

      Thanks&Regards

      Sridevi.G