$.ajax()语法
$.ajax({
async: true, // 表示请求是否异步处理。默认是 true。
type: "POST", // 规定请求的类型(GET 或 POST)。
url: url, // 规定发送请求的 URL。默认是当前页面。
timeout: 1000, // 设置本地的请求超时时间(以毫秒计)。
data: , // 规定要发送到服务器的数据。
dataType: "json", // 预期的服务器响应的数据类型。
contentType: , 发送数据到服务器时所使用的内容类型。默认是:"application/x-www- form-urlencoded"。
beforeSend(xhr), // 发送请求前,返回结果前运行的函数。
success(result,status,xhr), // 当请求成功时运行的函数。
error(xhr,status,error), // 若是请求失败要运行的函数。
complete(xhr,status), 请求完成时运行的函数(在请求成功或失败以后均调用,即在 success 和 error 函数以后)。
});
复制代码
ajaxSetup() 方法,设置全局ajax默认值
$.ajaxSetup({
timeout: 1000*60,
complete: function(XMLHttpRequest, textStatus) {
if (textStatus == 'timeout') {
$.modal.alertWarning("服务器超时,请稍后再试!");
$.modal.closeLoading();
} else if (textStatus == "parsererror") {
$.modal.alertWarning("服务器错误,请联系管理员!");
$.modal.closeLoading();
}
}
});
复制代码
.getJSON()、$.post()
$.get(
URL,
data,
function(data,status,xhr){
// 请求成功后所执行的函数
// status - 包含请求的状态 ("success"、"notmodified"、"error"、"timeout"、"parsererror")
}),
dataType,
复制代码
简单对象
$.ajax({
type: "POST",
url: prefix +"/syncAllWxUsers",
dataType: "json",
data: {"openId":wxUser.openId},
success: function (data) {
layer.msg(data.msg);
$.table.refresh();
}
});
// 后台方法
public ResultData<Boolean> syncWxUser(String openId)
throws WxErrorException {
return ;
}
复制代码
复杂对象
$.ajax({
type: "POST",
url: prefix +"/syncOrders",
data: JSON.stringify(tbkOrder),
dataType: "json",
contentType : "application/json",
success: function (data) {
layer.msg(data.msg);
$.table.refresh();
}
});
// 后台方法
public ResultData<Boolean> syncOrders(@RequestBody TbkOrder tbkOrder) throws IOException, WxErrorException {
return ResultData.result(tbkOrderService.syncSettleOrder(tbkOrder));
}
复制代码