xx.js

 

this.jsonp({
async: false,
url: "http://123.56.240.111/mobile/login/get_sit_state", //请求地址
type: "GET", //请求方式
data: { sit_id: "3",store_id:store_id}, //请求参数
dataType: "JSONP",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
// jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也
callback:"process",
success: function (Response) {
alert(Response);

},
fail: function (status) {
console.log(status);
},
});

 

jsonp:function (options) {
options = options || {};
if (!options.url || !options.callback) {
throw new Error("参数不合法");
}

//创建 script 标签并加入到页面中
var callbackName = ('jsonp_' + Math.random()).replace(".", "");
var oHead = document.getElementsByTagName('head')[0];
options.data[options.callback] = callbackName;
var params = this.formatParams(options.data);
var oS = document.createElement('script');
oHead.appendChild(oS);

//创建jsonp回调函数
window[callbackName] = function (json) {
oHead.removeChild(oS);
clearTimeout(oS.timer);
window[callbackName] = null;
options.success && options.success(json);
};

//发送请求
oS.src = options.url + '?' + params;
//超时处理
if (options.time) {
oS.timer = setTimeout(function () {
window[callbackName] = null;
oHead.removeChild(oS);
options.fail && options.fail({ message: "超时" });
}, time);
}
},

//格式化参数
formatParams:function (data) {
var arr = [];
for (var name in data) {
arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
}
return arr.join('&');
}

访问的页面要调用js中的函数

xx.php

$callback = $_GET['process'];
$data = $arr;
echo 'this.'.$callback.'('.json_encode($data).');';die();