jQuery Ajax - ajax()方法,参数注释
ajax(参数注释,解答):
$.ajax({
// type,请求方式
type: "get",
// url,地址,就是action请求路径
url: "http://localhost:8080/inventorys/",
// async,同步或异步,默认值:true(异步请求)
async: false,
// cache,默认值: true,dataType 为 script 和 jsonp 时默认为 false。设置为 false 将不缓存此页面。
cache: true,
// contentType,默认值: "application/x-www-form-urlencoded"。发送信息至服务器时内容编码类型。默认值适合大多数情况。如果你明确地传递了一个 content-type 给 $.ajax() 那么它必定会发送给服务器(即使没有数据要发送)。
contentType: "application/x-www-form-urlencoded",
// data,参数,请求可以不填写,上传时填写上传参数,格式为:Key/Value 格式,参数名与后台商定
data: {},
// 跨域都得要dataType,jsonpCallback
// dataType,数据类型text xml json script jsonp,jsonp跨域请求,就需要jsonpCallback回调"结果",结果可以去netword中看结果是什么name等;
// 可用值:
// "xml": 返回 XML 文档,可用 jQuery 处理。
// "html": 返回纯文本 HTML 信息;包含的 script 标签会在插入 dom 时执行。
// "script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了 "cache" 参数。注意:在远程请求时(不在同一个域下),所有 POST 请求都将转为 GET 请求。(因为将使用 DOM 的 script标签来加载)
// "json": 返回 JSON 数据 。
// "jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。
// "text": 返回纯文本字符串
dataType: "jsonp",
// jsonpCallback,为 jsonp 请求指定一个回调函数名。这个值将用来取代 jQuery 自动生成的随机函数名。这主要用来让 jQuery 生成度独特的函数名,这样管理请求更容易,也能方便地提供回调函数和错误处理。你也可以在想让浏览器缓存 GET 请求的时候,指定这个回调函数名。
jsonpCallback: "inventorys",
// beforeSend,这个方法可以使用在,当请求时间过长时取消请求等。
beforeSend: function() {
console.log("加载中...");
},
// 请求成功
success: function(res){
// 成功后做一些操作
console.log(res);
var SEIRI = "";
$('#inventoryTable').empty().append(SEIRI);
},
// 请求失败
error: function() {
consule.log('fail');
}
});
常用ajax(直接copy):
$.ajax({
type: "get",
url: "http://localhost:8080/inventorys/",
data: {},
timeout: 5000, // 超时时间
contentType: "application/x-www-form-urlencoded; charset=utf-8", // 解决传递中文参数乱码问题
// 请求等待
beforeSend: function() {
console.log("加载中...");
},
// 请求成功
success: function(res){
// 成功后做一些操作
console.log(res);
var SEIRI = "";
$('#inventoryTable').empty().append(SEIRI);
},
// 请求失败
error: function() {
consule.log('fail');
}
});
常用2:
$.ajax({
url: '',
type: 'POST', //GET
data:{
name:'llc',
age:22
},
timeout: 5000, // 超时时间
dataType:'json', // 返回的数据格式:json/xml/html/script/jsonp/text
contentType: "application/x-www-form-urlencoded; charset=utf-8", // 解决传递中文参数乱码问题
beforeSend: function(xhr){
console.log(xhr)
console.log('等待中...');
},
success: function(res, textStatus, jqXHR){
console.log(res);
var SEIRI = "";
$('#inventoryTable').empty().append(SEIRI);
},
error: function(err, textStatus){
console.log('错误',err.responseText);
console.log(err);
console.log(textStatus);
}
})