异步请求,跨域请求ajax
2019-01-14 本文已影响22人
望月成三人
(function ($) {
$.rpc = {};
$.rpc.setup = function (settings) {
$.extend($.rpc.defaults, settings);
};
$.rpc.defaults = {
// remoteHost:"http://test.wsd.com:8081/catering_oss",
remoteHost:"http://liao-pc.wsd.com:8080/catering_oss",
module:"/waiterIpad",
urlExt:"",
httpMethod:"get",
dataType:"jsonp"
};
$.rpc.req = function (action, data, cb) {
var settings = {};
$.extend(settings, $.rpc.defaults);
if (typeof action === "object") {
$.extend(settings, action)
} else {
settings.action = action;
}
$.ajax({
url:settings.remoteHost + settings.module + "/" + settings.action + settings.urlExt,
type:settings.httpMethod,
dataType:settings.dataType,
jsonp:'jsoncallback',
timeout:500,
error:function (xhr) {
if ($.isFunction(cb)) {
cb(null);
}
},
data:data,
success:function (data) {
if ($.isFunction(cb)) {
cb(data);
}
}
});
};
$.fn.showTime = function (tpid) {
var showTimeFn = function () {
var today = new Date();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
var timeStr = today.getFullYear() + "-" + today.getMonth() + "-" + today.getDate() + " " + hours + ":" + minutes + ":" + seconds;
$('#' + tpid).html(timeStr);
}
showTimeFn();
setInterval(showTimeFn, 1000)
}
})(jQuery);
- 调用
初始化: $.rpc.setup({"module":"","urlExt":".json","remoteHost":"http://test.wsd.com:8081/catering_oss/manager",dataType:"jsonp"});
发送请求
$.rpc.req("getEmployeeList",{"currentPage":pageIndex+1,"role":role,"staffName":staffName},function(resp){
var employee={"data":{}};
for(var key in resp.data.item){
employee.data[key]=employee.data[key]||{};
employee.data[key]=resp.data.item[key];
employee.data[key].source="server";//标识数据来源
}