cancel
Showing results for 
Search instead for 
Did you mean: 

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

former_member184238
Participant
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member184238
Participant
0 Kudos

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

former_member189929
Participant
0 Kudos

Hi

If you use like below code

var sServiceUrl = "http://xxxx:8000/sap/opu/odata/EAE/SERVICE_ENTRYSHEET_SRV";

 

 

 

                                        var supstr11="PurchaseOrderItems";

///////////////////////////// here false will give the out put in xml format if it is true means it will give json format out put at my knowledge/////////////////////////

                                        var oModel4 = new sap.ui.model.odata.ODataModel(sServiceUrl, false);

 

                                        oModel4.setHeaders(

 

                                                            {

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

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

                               "DataServiceVersion": "2.0",      

                               "X-CSRF-Token":"Fetch" 

 

                                                            }

 

                                        );

 

                                                  oModel4.read(supstr11, oEntry, null, function(oEntry,response){

 

                                                  sap.ui.commons.MessageBox.alert("read successful");

                                               

                                            },function(){

                                        

                                                      sap.ui.commons.MessageBox.alert("read failed");

                                                 

                                            });

former_member184238
Participant
0 Kudos

hi,

But I am using json model. I already applied the same code for the JSON model.Then I am getting XMl parser Error.

Is there any another way for the JSON model?

Thanks&Regards

Sridevi.G

former_member189929
Participant
0 Kudos

can i have a detail error screen shot .....

In chrome press ctrl+shift+i and go to network tab and check the error there...

former_member184238
Participant
0 Kudos

Actually in my application I am using JSON model.

when I apply same code for this I am getting HTTP request failed error in console and in Network Preview I am getting the following error message and the request doesn't triggering the sap back end.

CX_SXML_PARSE_ERROR/001560AA0E081DEB8CA398CC1690D406Error while parsing an XML stream60A431E3D40EF1319B1D20CF30C48576

In my desktop application I used oData model.Then it's working fine.But for JSON model I am getting this error.

Thanks&Regards

Sridevi.G

former_member189929
Participant
0 Kudos

it means you are sending date for unknown variable to your service pls check that request data 

former_member184238
Participant
0 Kudos

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

former_member189929
Participant
0 Kudos

Hi

try this

oModel.setHeaders(

 

                                                            {

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

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

                               "DataServiceVersion": "2.0",      

                               "X-CSRF-Token":"Fetch" 

 

                                                            }

 

                                        );