通过Google找到的一个原生js解决jsonp请求的方案:
var native_jsonp = (function () {
var that = {};
that.send = function (src, options) {
var options = options || {},
callback_name = options.callbackName || 'callback',
on_success = options.onSuccess || function () {},
on_timeout = options.onTimeout || function () {},
timeout = options.timeout || 10;
var timeout_trigger = window.setTimeout(function () {
window[callback_name] = function () {};
on_timeout();
}, timeout * 1000);
window[callback_name] = function (data) {
window.clearTimeout(timeout_trigger);
on_success(data);
};
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = src;
document
.getElementsByTagName('head')[0]
.appendChild(script);
};
return that;
})();
native_jsonp.send(_url+'&callback=callback', {
callbackName: 'callback',
onSuccess: function (json) {
console.log('请求成功',json); },
onTimeout: function () {
console.log('请求超时'); },
timeout: 5
});