I'm trying to call an OData service using JQuery $.ajax and running into some issues.
When I call the service with dataType: "jsonp", I get a status code of 200 and the data I need but it falls into my JQuery error: function(data)
When I call the service with dataType: "json", I get nothing, the call to the service does not even happen.
Here is my .ajax call:
$.ajax({
beforeSend: function(request) {
request.setRequestHeader("Accept", "application/json;charset=utf-8");
},
type: "GET",
url: this.uri + filter,
dataType: "jsonp",
success: function(data) {
// I never get here but in fiddler I get a 200 status code
},
error: function(data) {
// This works and gives me the data but it's in the JQuery error handler
// $.parseJSON(data.responseText)
}
});
I've tried several variations "jsonp: false", "callback", etc and without success. I should also mention that the website and odata webapi are on the same server but the website is accessed via https and then the client ajax calls the service via http.
Would anyone be able to tell me where I'm going wrong?
Thanks in advance!
After countless hours of effort I finally got it to work by skipping JQuery and using xmlhttprequest object, here's my working code:
var uri = this.uri + filter;
var http_request = new XMLHttpRequest();
http_request.setRequestHeader("Authorization", "Negotiate");
http_request.onreadystatechange = function() {
if (http_request.readyState == 4 ) {
var data = JSON.parse(http_request.responseText);
alert(data.value[0].Name);
}
}
http_request.open("GET", uri, true);
http_request.send();
I borrowed pieces from everywhere on the web so others deserve the credit.
This is for use on a local intranet running IE only, if anyone has another suggestion I can try or a better way of doing this I would appreciate your feedback.
Thanks!