0

I'm writing a jQuery interface to use a couple of OData services created in SAP. The services is working ok, and are also being used by some other applications.

I've already searched and I'm mostly come across people who are saying it's a cross domain issue. The first thing I tried was to plain and simply do a cross domain ajax call:

$.ajax({
    url : 'http://saphostname:8000/sap/opu/odata/sap/entityset/?$format=json',
    crossDomain : true,
    xhrFields {
      withCredentials : true,
    }
    // .. success, statusCodes, whatever ..
})

The responses that came from this call were 200, and if I viewed the content in the developer tools I saw my json message as I would expect it to be in there, but none of my callback functions were being triggered.

Upon searching more, somebody suggested using $.getJSON(). This didn't work either. The error that kept coming back was 401, so I assumed it was a cross domain issue.

The last thing I stumbled upon was JSONP. The response is coming back with an OK status code, and once again if I view the body content I see my json string as I would expect it. The only problem is my browser says there is a syntax error in the response text.

enter image description here

enter image description here

All of my search results have brought up issues regarding cross domain requests, and errors resulting there-in. Maybe it is the issue, but because I'm getting the results back that I would expect in the responses, I'd have to assume that connectivity is no problem.

tl;dr: ajax cross-domain requests are successful but don't trigger callback functions and jsonp gives me a syntax error. I'm not sure where to keep looking.

Bryan Abrams
  • 327
  • 1
  • 7
  • Any chance for the callback function really have a JS error? You can confirm that the function works fine despite of JSONP, by simply passing a similar dummy json string data from a dummy service. – Hari Apr 03 '14 at 12:48
  • I kept it rather simple, just an alert('hi') call inside. I don't see any other errors popping up in the log. – Bryan Abrams Apr 03 '14 at 12:53
  • JSON != JSONP. Please understand [how JSONP works](https://en.wikipedia.org/wiki/JSONP) and notice the missing padding. – Bergi Apr 03 '14 at 12:56
  • What CORS headers are you sending witht he JSON? Looks like only the credentials are not working. – Bergi Apr 03 '14 at 12:57
  • I only used JSONP because it was a suggestion that kept popping up everywhere. What I notice is that the data I want is coming back from both requests, I just can't get to it. As far as header data is concerned, I just did `crossDomain:true, xhrFields{withCredentials:true}` with ajax. – Bryan Abrams Apr 03 '14 at 13:00

2 Answers2

1

You are trying to do a JSONP request to a JSON service. The way that the response is handled for a JSONP request is that it is executed, but executing a JSON response only evaluates it and discards it.

The crossDomain property only causes the request to act like a cross domain request, i.e. using JSONP, regardless if it's actually to a different domain or not.

You should specify dataType: 'json' in the properties, to keep it from using JSONP.

Alternatively, if the service also supports JSONP, you could change $format=json in the URL to $format=jsonp, and specify dataType: 'jsonp' in the properties.

Edit:

Another thing that you can try is to use a proxy that turns a JSONP request to a JSON request. I have set up such a proxy that you can use to test if you can get the right response:

$.get(
  "http://jsonp.guffa.com/Proxy.ashx?url=" + escapeURIComponent("saphostname:8000/sap/opu/odata/sap/entityset/?$format=json"),
  function(data) {
    console.log(data);
  },
  "jsonp"
);
Community
  • 1
  • 1
Guffa
  • 640,220
  • 96
  • 678
  • 956
0

I already had a problem like this before and what helped me to solve the problem was using callbacks like these:

// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });

// Perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second complete" );
});

Now you can see which one is being triggered and debug the problem.

Igor Escobar
  • 927
  • 1
  • 9
  • 13
  • Firefox/chrome report 200 from the service, and .fail() is triggered. My error message is "error" and the status code is 0. I'll try with the other options on as well first. – Bryan Abrams Apr 03 '14 at 13:56
  • Take a look at: http://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0 maybe it should help. – Igor Escobar Apr 03 '14 at 13:58
  • Thanks, I'll read over these. I gave up for now (work day over) but I'll try again tomorrow. – Bryan Abrams Apr 03 '14 at 15:40