JavaScript is simulated to implement jsonp
- 2020-06-15 07:46:40
- OfStack
function prescript(s) {
if (s.cache === undefined) {
s.cache = false;
}
if (s.crossDomain) {
s.type = "GET";
}
}
function prejsonp(s, originalSettings, jqXHR) {
// Name the callback function
var callbackName = s.jsonpCallback
s.url += (/(?:)/.test(s.url) ? "&" : "?") + s.jsonp + "=" + callbackName;
// The script executes and USES a data converter to retrieve it json
// Data is provided to the code acquisition server
s.getData = function() {
if (!responseContainer) {
jQuery.error(callbackName + " was not called");
}
return responseContainer[0];
};
// Modification handling mechanism
s.dataTypes[0] = "json";
// create 1 A global function
overwritten = window[callbackName];
// Used to collect data from the server
window[callbackName] = function() {
responseContainer = arguments;
};
return "script";
}
/**
* jsonp Preprocessing
*/
function inspectPrefiltersOrTransportsA(options, originalOptions, jqXHR) {
// pretreatment jsonp
var dataTypeOrTransport = prejsonp(options, originalOptions, jqXHR)
// expand dataTypes
options.dataTypes.unshift(dataTypeOrTransport);
// pretreatment script type
prescript(options)
}
/**
* The dispenser
* @return {[type]} [description]
*/
function inspectPrefiltersOrTransportsB(s, originalOptions, jqXHR) {
return {
send: function(_, complete) {
var script = jQuery("<script>").prop({
async: true,
charset: s.scriptCharset,
src: s.url
}).on(
"load error",
callback = function(evt) {
script.remove();
callback = null;
if (evt) {
complete()
}
}
);
//<script async="" src="http://192.168.1.113:8080/github/jQuery/jsonp.php
document.head.appendChild(script[0]);
}
}
}
/**
* simulation ajax the jsonp request
* @param {[type]} options [description]
* @return {[type]} [description]
*/
function createAjax(options) {
if (typeof url === "object") {
options = url;
url = undefined;
}
options = options || {};
/**
* parameter
* jQuery.ajaxSetup Is the default parameter
* @type {[type]}
*/
var s = jQuery.ajaxSetup({}, options);
// Deferreds
// Asynchronous mechanisms
var deferred = jQuery.Deferred();
var completeDeferred = jQuery.Callbacks("once memory");
/**
* Actual returned ajax object
* @type {Object}
*/
var jqXHR = {}
// the jqXHR Transformation of object promise Object, join complete , success , error methods
deferred.promise(jqXHR).complete = completeDeferred.add;
// The alias
jqXHR.success = jqXHR.done;
jqXHR.error = jqXHR.fail;
s.dataTypes = jQuery.trim(s.dataType || "*").toLowerCase().match(/(?:)/) || [""];
// pretreatment
inspectPrefiltersOrTransportsA(s, options, jqXHR);
for (i in {
success: 1,
error: 1,
complete: 1
}) {
jqXHR[i](s[i]);
}
/**
* The dispenser
*/
transport = inspectPrefiltersOrTransportsB(s, options, jqXHR);
function done(status, nativeStatusText, responses, headers) {
console.log(s,s.getData())
}
// Send the request
transport.send(s, done);
return jqXHR;
}
function show(data){
$('body').append('<li>'+ data +'</li>');
}
/**
* Data callback receiving
* @return {[type]} [description]
*/
function flightHandler(){
}
$("#test").click(function(){
// perform 1 An asynchronous HTTP ( Ajax ).
var ajax = createAjax({
url: 'http://192.168.1.113:8080/github/jQuery/jsonp.php',
data: {
'action': 'aaron'
}, // An array of prepassed parameters
dataType: 'jsonp', // The data type
jsonp: 'callback', // Specifies the name of the callback function that is received on the server side 1 Send and pass back
jsonpCallback:flightHandler,
success: function() {
show(' Local events success')
}
})
})