Reputation: 441
I am making a AJAX call using JSONP which is failing with no reported reason. We are watching what's happening on the server which is being called, and see that AJAX is adding to the URL, which may be adding to our issues.
The returning data should be JSON. When calling the URL from a browser, it returns as expected, when from a page calling the below code, an error of "Undefined" is returned, suggesting there is no error reported.
$.ajax({
url: 'http://foo:8080/playlist',
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
jsonp : false,
success: function(data){
//Writes return data to UI
writeToUI(text.html(JSON.stringify(data)));
},
error: function(e){
alert(e.message);
}
});
When watching the server being called, we see that the URL is now "http://foo:8080/playlist?._=1449088167560".
We think that the extra information on the URL could be causing the API to fail on the server, but want to see where this is coming from, and if there is a way to turn it off?
Ideas?
Steve
Upvotes: 4
Views: 3347
Reputation: 12508
The _=1449088167560
is coming from jQuery because you specified the datatype
as jsonp
. See the jQuery.ajax docs here for more information. Specifically look at the cache
parameter description:
cache (default:
true, false for dataType 'script' and 'jsonp'
) Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.
Because you're using jsonp
jQuery is automatically appending this parameter to prevent the web results from being cached which normally occurs for GET
requests. That's where the _=1449088167560
comes from. I've never looked at that code internally in jQuery but I believe that it generates this number from a timestamp (perhaps a UNIX timestamp value).
As to fixing this, I guess you could try specifically setting the cache
parameter to true
such as this:
$.ajax({
url: 'http://foo:8080/playlist',
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
jsonp : false,
cache: true,
success: function(data){
//Writes return data to UI
writeToUI(text.html(JSON.stringify(data)));
},
error: function(e) {
alert(e.message);
}
});
This is the default for normal GET
requests and should prevent the extra parameter from being appended.
Another thing to check is the apparent .
value hidden between the ?
and _
values in your url: http://foo:8080/playlist?._=1449088167560. I'm not sure if that was a typo from when you pasted it in your question, but it definitely could be causing your error. Typically, unless the server is looking specifically for a parameter who's key is _
, having the extra parameter in the url should make no difference. With the .
however, I think that's technically an invalid URI string.
Upvotes: 6