APP开发时 是不需要JSONP支持的 mui本身就支持 但是 如果是微信或者浏览器的普通模式就不支持。所以贴出以下代码 大家可参考使用。(参考了zepto)
var jsonpID = 0;
$.ajaxJSONP = function(url, options){
if (typeof url === "object") {
options = url;
url = undefined;
}
var options = $.extend({}, options || {})
for (var key in $.ajaxSettings) {
if (options[key] === undefined) {
options[key] = $.ajaxSettings[key];
}
}
options.url = appendQuery(options.url, '_=' + Date.now())
options.url = appendQuery(options.url,
options.jsonp ? (options.jsonp + '=?') : options.jsonp === false ? '' : 'callback=?')
var _callbackName = options.jsonpCallback,
callbackName = ($.isFunction(_callbackName) ?
_callbackName() : _callbackName) || ('jsonp' + (++jsonpID)),
script = document.createElement('script'),
originalCallback = window[callbackName],
responseData,
abort = function(errorType) {
script.parentNode.removeChild(script)
if (callbackName in window) window[callbackName] = empty
ajaxComplete('abort', xhr, options)
},
xhr = { abort: abort }, abortTimeout;
if (options.error) script.onerror = function() {
xhr.abort()
options.error()
}
window[callbackName] = function(data){
clearTimeout(abortTimeout)
script.parentNode.removeChild(script)
delete window[callbackName]
ajaxSuccess(data, xhr, options)
}
serializeData(options)
script.src = options.url.replace(/=\?/, '=' + callbackName)
document.head.appendChild(script)
if (options.timeout > 0) abortTimeout = setTimeout(function(){
xhr.abort()
ajaxComplete('timeout', xhr, options)
}, options.timeout)
return xhr
}
4 个回复
davetang
支持一个,非常好用
世界充满贰
不错。
云雾
新手求教一下,请问这个怎么用呀?
9***@qq.com
怎么使用啊