0

I am trying to do AJAX using jQuery with an ODATA REST service provided by an SAP backend system.

          $.ajax({
                type: 'GET',
                url: 'http://sapxx.sapms/sap/opu/odata/.../',                   
                async: true,
                dataType: 'jsonp',                  
                username: 'username',
                password: 'password',
                crossDomain: true,                  
                success: function() {
                    alert('Done.');
                },
                error: function() {
                    alert('Error.');
                },
            });

The request returns status 200 and I get a response from the server which is called something like "?callback=jQuery3100687..." and contains xml code. All this is visible in the Chrome debugger. But after the successful HTTP Request I get the aforementioned error

Uncaught SyntaxError: Unexpected token <

I suspect the error is due to the "dataType" parameter which is set to "jsonp" in the request. Is there any way to work around this error? The server can only respond using XML format. The request only works when the dataType is set to "jsonp", I guess because it enables CORS. After sending the request, I get the "Error" alert despite the 200 status.

doktormatte
  • 11
  • 1
  • 8
  • You've invalid data response, review your response. – Govind Samrow Aug 23 '16 at 11:07
  • http://stackoverflow.com/questions/24377804/cross-domain-jsonp-xml-response – Karol Klepacki Aug 23 '16 at 11:09
  • You can not magically have the server change the response to JSONP. The server would need to do it. The Only work around for CORS is using a proxy on your server or a proxy on some other server that sets CORS headers or returns JSONP. – epascarello Aug 23 '16 at 11:23
  • Is there no way to just extract the XML content from the response? I can see the response in the debugger, shouldn't there be a way to just access it despite the XML problem? – doktormatte Aug 23 '16 at 11:37

1 Answers1

1

You say you get an xml response from the server. Your ajax request is set up for jsonp, though:

dataType: 'jsonp',

Try this instead:

dataType: 'xml',
success: function(xml) {
    //remainder of the code
}

If you set the dataType to jsonp, jQuery will try to parse the response as JSON. This results in errors, because the response ins't JSON.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • When I set the dataType to XML the request stops working, I get "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:xxxxxx' is therefore not allowed access. The response had HTTP status code 401." – doktormatte Aug 23 '16 at 11:14
  • You can't use jsonp with XML. It's just not possible, unless you [write a ugly hack](http://stackoverflow.com/a/10069132/1835379) to make it work. Either get the data returned as JSON, or find a workaround for the CORS issue. – Cerbrus Aug 23 '16 at 11:18
  • But I get a response and it contains the XML code that I am looking for. Is there no way to just tell my webapp that the response is XML and not jsonp? – doktormatte Aug 23 '16 at 11:46
  • @doktormatte Either you change your server response to JSON, or you use other ways to avoid cross-domain calls. Consider to use server-side calls instead (more robust). – Raptor Aug 24 '16 at 02:14
  • Well, I've made some progress and now the server response is in JSON format. The error message has now changed to: "Uncaught SyntaxError: Unexpected token :" – doktormatte Aug 24 '16 at 11:44
  • @doktormatte: Then the JSON format isn't correct. Are you returning jsonp or json? – Cerbrus Aug 24 '16 at 12:05
  • It is now returning JSON and I guess that is a problem. – doktormatte Aug 24 '16 at 12:26
  • Then set the datatype to json, or format the request as jsonp – Cerbrus Aug 24 '16 at 13:30
  • @Cerbrus: Do you have a link to a description about how to do that? I do not know how to format the request as jsonp without setting the dataType to jsonp. – doktormatte Aug 24 '16 at 16:35