Skip to content

Commit

Permalink
ajax: add support for 'jsonp' dataType
Browse files Browse the repository at this point in the history
Closes #367
  • Loading branch information
mislav committed Apr 6, 2012
1 parent 3eff37c commit 7572f37
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
16 changes: 12 additions & 4 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
xmlTypeRE.test(mime) && 'xml' ) || 'text'
}

function appendQuery(url, query) {
return (url + '&' + query).replace(/[&?]{1,2}/, '?')
}

$.ajax = function(options){
var settings = $.extend({}, options || {})
for (key in $.ajaxSettings) if (settings[key] === undefined) settings[key] = $.ajaxSettings[key]
Expand All @@ -152,16 +156,20 @@
if (!settings.crossDomain) settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) &&
RegExp.$2 != window.location.host

if (/=\?/.test(settings.url)) return $.ajaxJSONP(settings)
var dataType = settings.dataType, hasPlaceholder = /=\?/.test(settings.url)
if (dataType == 'jsonp' || hasPlaceholder) {
if (!hasPlaceholder) settings.url = appendQuery(settings.url, 'callback=?')
return $.ajaxJSONP(settings)
}

if (!settings.url) settings.url = window.location.toString()
if (settings.data && !settings.contentType) settings.contentType = 'application/x-www-form-urlencoded'
if (isObject(settings.data)) settings.data = $.param(settings.data)

if (settings.type.toLowerCase() == 'get' && settings.data)
settings.url = (settings.url + '&' + settings.data).replace(/[&?]{1,2}/, '?')
settings.url = appendQuery(settings.url, settings.data)

var mime = settings.accepts[settings.dataType],
var mime = settings.accepts[dataType],
baseHeaders = { },
protocol = /^([\w-]+:)\/\//.test(settings.url) ? RegExp.$1 : window.location.protocol,
xhr = $.ajaxSettings.xhr(), abortTimeout
Expand All @@ -179,7 +187,7 @@
clearTimeout(abortTimeout)
var result, error = false
if ((xhr.status >= 200 && xhr.status < 300) || (xhr.status == 0 && protocol == 'file:')) {
var dataType = settings.dataType || mimeToDataType(xhr.getResponseHeader('content-type'))
dataType = dataType || mimeToDataType(xhr.getResponseHeader('content-type'))
result = xhr.responseText

try {
Expand Down
25 changes: 20 additions & 5 deletions test/ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h1>Zepto Ajax unit tests</h1>
t.pause()
var xhr = $.getJSON('fixtures/jsonp.js?callback=?&timestamp='+(+new Date), function(data){
deferredResume(t, function(){
this.assertEqual(data.items.length, 0)
this.assertEqual('world', data.hello)
this.assertEqual(0, $('script[src^=fixtures]').size())
})
})
Expand All @@ -120,12 +120,27 @@ <h1>Zepto Ajax unit tests</h1>
},

testAjaxGetJSONPErrorCallback: function(t){
var successFired, errorFired, xhr = $.ajax({
t.pause()
$.ajax({
type: 'GET',
url: 'fixtures/404.js?callback=?&timestamp='+(+new Date),
url: 'fixtures/404.js?timestamp='+(+new Date),
dataType: 'jsonp',
success: function() { t.refute(true) },
error: function() { t.assert(true) },
complete: function() { deferredResume(t) }
})
},

testJSONPdataType: function(t){
t.pause()
$.ajax({
dataType: 'jsonp',
success: function() { t.refute(true); },
error: function() { t.assert(true); }
url: 'fixtures/jsonp.js?timestamp='+(+new Date),
success: function(data){
deferredResume(t, function(){
this.assertEqual('world', data.hello)
})
}
})
},

Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/jsonp.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
jsonp1({"items":[]})
for (key in window) {
// we can't read callback from URL, hence this hack
if (/^jsonp(\d+)$/.test(key)) {
window[key]({ hello: 'world' })
}
}
delete window.key

0 comments on commit 7572f37

Please sign in to comment.