0

I have a SAP Gateway OData-Service and a local Tomcat Apache Server. My SAPUI5 Client is deployed in the Tomcat and requests a OData-Webservice from the SAP Gateway remote server. In fact there is a cross origin domain error. So I set the header "Access-Control-Allow-Origin" in my OData-Webservice and my SAPUI5 client requests with JSONP, but I will get an error because the SAP Gateway can't handle with JSONP responses.

My code: enter image description here

The error: "Uncaught SyntaxError: Unexpected token :"

The error depends on the incompatibility of the SAP Gateway to JSONP.

If I look in the network requests I will find this one: enter image description here

It is the JSON (not JSONP) response from the webservice.

So my question. Is there a possibility to access to this response? I tried to access via a lot of callbacks like success, error, fail, done, always, complete, and so on. But no chance...

thanks and best regards

iach
  • 247
  • 4
  • 18

2 Answers2

0
$.ajax({
   type: 'GET',
    url: url,
    async: false,
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(response) {
       //this is where you'll get data/response
       console.log(response) ;
   },
    error: function(e) {
       console.log(e.message);
    }
});

Update:

Problem is with your context.. there is some value like _1453458442107=:1 enter image description here

Check that and encode it.

Sunil B N
  • 3,866
  • 1
  • 25
  • 46
0

If you set the CORS header "Access-Control-Allow-Origin" in the odata responses you should be able to access the service with regular JSON (without the P).

JSONP is a workaround to circumvent the same origin policy when the CORS-Headers are not available.

If your odata service uses authentification it might be necessary to set some more CORS headers to get it working.

On MDN you can read more about CORS and its headers.

On Wikipedia is a chapter about CORS vs JSONP.

So have you tried something like the following?

$.get({
    url:"https://.../sap/opu/odata/sap/ZMOBILAD_SRV/UsernameSet?$format=json",
    context: document.body,
    cache: false
    }).done(function(){ 
        console.log(this); 
    });
schnoedel
  • 3,770
  • 1
  • 10
  • 16
  • Hi I tried the CORS Plugin for chrome. this does not work. My solution was this one: http://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome – iach Feb 05 '16 at 14:55
  • You have to set the CORS Header in the responses of your webservice. Have a look at [this SCN article](http://scn.sap.com/community/gateway/blog/2014/09/23/solve-cors-with-gateway-and-chrome) especially the chapter "The Server Side" – schnoedel Feb 05 '16 at 14:59