//jquery ajax常用参数: $.ajax({ url:"", // ajax 请求地址 type:"GET",//请求方式 ’GET‘或’POST‘,默认为’GET‘。 dataType:"json",//根据返回数据类型,可以有如下数据可选:xml html script json jsonp text data:{}, //发送到服务器的数据,可以直接传对象{code: 456},如果是get请求会自动拼接到url后面,如:&code=456; //请求成功后的回调函数 success: function(data){ }, //请求失败时调用此函数。有以下三个参数:XMLHttpRequest 对象、错误信息、(可选)捕获的异常对象。 error: function (XMLHttpRequest, textStatus, errorThrown) { console.log(textStatus);// 打印错误信息 }});
//jquery ajax发送一个get请求 $.ajax({ type: "GET", url: "data.php", dataType: 'json', // data: {code: 456},//也可以是字符串链接"code = 456",建议用对象 data:'code=456', success: function (data) { console.log("返回的数据: "); console.log(data); } });//jquery ajax发送一个post请求 $.ajax({ type: "POST", url: "data.php", dataType: 'json', // data: {code: 456},//也可以是字符串链接"code = 456",建议用对象 data:'code=456', success: function (data) { console.log("返回的数据: "); console.log(data); } });//jquery ajax发送一个get请求,简写方式:其实就是对ajax的二次封装, //$.get("请求url","发送的数据对象","成功回调","返回数据类型"); $.get("data.php",{code: 456},function(data) { console.log("返回的数据: "); console.log(data); },'json');//jquery ajax发送一个post请求:其实就是对ajax的二次封装 $.ajax({ type: "POST", url: "data.php", dataType: 'json', // data: {code: 456},//也可以是字符串链接"code = 456",建议用对象 data:'code=456', success: function (data) { console.log("返回的数据: "); console.log(data); } });
jquery ajax 请求方式及常用参数
最新推荐文章于 2024-07-15 22:52:27 发布
本文介绍了如何使用 jQuery 的 AJAX 方法进行 GET 和 POST 请求,并展示了如何设置请求参数、处理响应数据及错误情况。此外还提供了 $.get 和 $.post 的简写方式。

375

被折叠的 条评论
为什么被折叠?



