Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Post oData method in Chrome through Integration Gateway

Hi all,

I tried to call post method of oData service that I created. I can post it through RestClient in Chrome which is working fine. Now I tried to call post method in Chrome via Javascript to test the service because we are developing HTML5 app in Cordova.

Before I called post method, I called GET method to get X-CSRF-Token but I didn't receive any requestHeaderResponse back from the GET method. The following is my source code.

var strUsername = "XXXX";

var strPassword = "XXXX";

var sUrl = "http://XXXX:8080/gateway/odata/sap/ZGW_SPICERS_CORE_NEW_SRV/CustomerCollection";

var oHeaders = {};

oHeaders['Authorization'] = "Basic " + btoa(strUsername + ":" + strPassword);

oHeaders['x-requested-with'] = "XMLHttpRequest";

oHeaders['Content-Type'] = "application/atom+xml";

oHeaders['DataServiceVersion'] = "2.0";

oHeaders['X-CSRF-Token'] = "fetch";

var request = { headers : oHeaders,

                requestUri : sUrl,

                method : "GET" };

var res;

OData.defaultHttpClient.enableJsonpCallback=false;

OData.read(request, function(data, response) {res = response;}, function(data) {alert(JSON.stringify(data))});

I tried to capture response data in res variable. I can see return data in JSON format but there was no header data!!

I used datajs api version 1.1.1 and 1.1.2 to consume oData but it returned similar header..... I looked at request response from Chrome and Gateway definitely returned back the header data.

Is there anyone had this issue before? It would be great if anyone could help..

Regards,

Pan

Tags was edited by: jitendra kansal

Tags:
Former Member
Former Member replied

Hi Bjoern,

I've finally solved it!!!!

Basically, I inserted to many header request when read oData. Here is my javascript code for reading oData

var strUsername = "XXX";

var strPassword = "XXXX";

var sUrl = "http://XXX:8080/gateway/odata/sap/ZGW_SPICERS_CORE_NEW_SRV/CustomerCollection";

var oHeaders = {};

oHeaders['Authorization'] = "Basic " + btoa(strUsername + ":" + strPassword);

//oHeaders['x-requested-with'] = "XMLHttpRequest";

//oHeaders['Content-Type'] = "application/atom+xml";

//oHeaders['DataServiceVersion'] = "2.0";

oHeaders['X-CSRF-Token'] = "fetch";

var request = { headers : oHeaders,

                requestUri : sUrl,

                method : "GET" };

var res;

OData.defaultHttpClient.enableJsonpCallback=false;

OData.read(request, function(data, response) {res = response;}, function(data) {alert(JSON.stringify(data))});

And also I've added this code to add Access-Control-Allow-Origin: '*' in return header request in get_entityset method of oData service class

DATA: ls_header TYPE ihttpnvp.
   ls_header-name = 'Access-Control-Allow-Origin' .
   ls_header-value = '*'.
   /iwbep/if_mgw_conv_srv_runtime~set_header( ls_header ).


So now when I executed read method in javascript, I finally got all headers data back

I believe this solution is similar to this blog from Alexander

http://scn.sap.com/community/gateway/blog/2014/02/28/gateway-the-icf-and-solving-the-same-origin-policy

Regards,

Pan

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question