当ajax请求jsonp时分两种情况:
1.跨域
跨域会进入到源码的 if(s.crossDomain)判断,在network中就会发现,源码创建了script标签并发送了请求。
代码中 script.remove(); 会在请求完删除,避免产生过多script
以下为jQuery源码部分
// Bind script tag hack transport
jQuery.ajaxTransport("script", function (s) {
// This transport only deals with cross domain requests
if (s.crossDomain) {
var script, callback;
return {
send: function (_, complete) {
script = jQuery("<script>").prop({
charset: s.scriptCharset,
src: s.url
}).on(
"load error",
callback = function (evt) {
script.remove();
callback = null;
if (evt) {
complete(evt.type === "error" ? 404 : 200, evt.type);
}
}
);
// Use native DOM manipulation to avoid our domManip AJAX trickery
document.head.appendChild(script[0]);
},
abort: function () {
if (callback) {
callback();
}
}
};
}
});
2.不跨越
不跨域不会出现script代码片段