The jQuery .ajax()
docs seem to suggest a request with a dataType
of json
request to an external domain will result in a JSONP call. (Please note bold)
"json"
: Evaluates the response as JSON and returns a JavaScript object. Cross-domain"json"
requests are converted to"jsonp"
unless the request includesjsonp: false
in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)
Below are some basic examples of how a JSON/JSONP API may be called. In most modern browsers with support for CORS all the requests will succeed. However, a basic JSON request will not be upgraded to a JSONP request as the docs seem to indicate.
(Note: The jsonp
setting for $.ajax
defaults to callback
)
Basic Cross Domain JSON Request (source)
$.ajax({
url: 'http://date.jsontest.com/',
dataType: 'json'
});
- This request fails in IE9 (Reason: "No Transport")
- This request will be successful when ran in a browser that supports CORS (Requested via the
XHR
transport)
JSON Request with Callback Parameter (source)
$.ajax({
url: 'http://date.jsontest.com/?callback=?',
dataType: 'json'
});
- This request will be successful in IE9
- This request uses the
<script>
transport even if the browser supports CORS - Is this the "upgrade" that the docs are referring to?
JSONP Request (source)
$.ajax({
url: 'http://date.jsontest.com/',
dataType: 'jsonp'
});
- This request will be successful in IE9
- This request uses the
<script>
transport even if the browser supports CORS