Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes to $.ajaxJSONP #660

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/ajax.js
Expand Up @@ -75,32 +75,40 @@

var callbackName = 'jsonp' + (++jsonpID),
script = document.createElement('script'),
abort = function(){
cleanup = function() {
clearTimeout(abortTimeout)
$(script).remove()
if (callbackName in window) window[callbackName] = empty
delete window[callbackName]
},
abort = function(){
cleanup()
ajaxComplete('abort', xhr, options)
},
xhr = { abort: abort }, abortTimeout

if (options.error) script.onerror = function() {
xhr.abort()
options.error()
serializeData(options)

if (ajaxBeforeSend(xhr, options) === false) {
ajaxError(null, 'abort', xhr, options)
return false
}

window[callbackName] = function(data){
clearTimeout(abortTimeout)
$(script).remove()
delete window[callbackName]
cleanup()
ajaxSuccess(data, xhr, options)
}

serializeData(options)
script.onerror = function() {
cleanup()
ajaxError(null, 'error', xhr, options)
}

script.src = options.url.replace(/=\?/, '=' + callbackName)
$('head').append(script)

if (options.timeout > 0) abortTimeout = setTimeout(function(){
xhr.abort()
ajaxComplete('timeout', xhr, options)
cleanup()
ajaxError(null, 'timeout', xhr, options)
}, options.timeout)

return xhr
Expand Down
24 changes: 24 additions & 0 deletions test/ajax.html
Expand Up @@ -134,6 +134,17 @@ <h1>Zepto Ajax unit tests</h1>
})
},

testAjaxGetJSONPBeforeSendCallback: function(t){
t.pause()
var xhr = $.ajaxJSONP({
url: 'fixtures/jsonp.js?callback=?&timestamp='+(+new Date),
beforeSend: function() {
t.assert(true)
deferredResume(t)
}
})
},

testAjaxGetJSONPErrorCallback: function(t){
t.pause()
$.ajax({
Expand All @@ -146,6 +157,19 @@ <h1>Zepto Ajax unit tests</h1>
})
},

testAjaxGetJSONPErrorWithoutErrorCallback: function(t){
t.pause()
$.ajax({
type: 'GET',
url: 'fixtures/404.js?timestamp='+(+new Date),
dataType: 'jsonp',
complete: function() {
t.assert(true)
deferredResume(t)
}
})
},

testJSONPdataType: function(t){
t.pause()
$.ajax({
Expand Down