on 09-19-2014 8:10 AM
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
Dear Pan,
Which SMP version are you using? If I recall correctly SP3 Patch Level 01 hat some issues with the CSRF token handling. So you might want to upgrade to SP4 in case you haven't done that already.
Kind regards
Björn
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Regards,
Pan
Above issue is quite similar to ours. But in our case we are using SOAP WS as one of our backend and we have modeled it as OData service using Integration gateway (SMP 3.0 SP03 PL02).
The same OData IGW URL has to be accessible directly from Java Script code and it would be a POST request. We are able to get the CSRF-TOKEN value in GET request. For POST request, Java script is not able to read the returned header from GET.
What do you suggest on this?
Regards
JK
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jitendra,
Please try adding this code at the top of get_entityset method.
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 ).
We found that although we can see the return header response from Browsers (Chrome or Firefox) but we cannot access it via Javascript because Access-Control-Allow-Origin header didn't get return or null.
The code that I added in the get_entityset method is to add Access-Control-Allow-Origin with value * in the header response. This will allow me to see headers data in javascript.
Please try it and let me know how it goes.
Cheers,
Pan
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.