{"id":2897,"date":"2023-11-14T14:45:52","date_gmt":"2023-11-14T06:45:52","guid":{"rendered":""},"modified":"2023-11-14T14:45:52","modified_gmt":"2023-11-14T06:45:52","slug":"Jquery ajax()\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/2897.html","title":{"rendered":"Jquery ajax()\u65b9\u6cd5"},"content":{"rendered":"
$.ajax({name:value, name:value, ... })
<\/span><\/code><\/pre>\n<\/div>\n\u53c2\u6570\u503c<\/h3>\n\n \u6b64\u65b9\u6cd5\u53ef\u4ee5\u4e3aAJAX\u8bf7\u6c42\u63d0\u4f9b\u591a\u4e2a\u540d\u79f0\/\u503c\u5bf9\u3002\u540d\u79f0\u548c\u503c\u5728\u4e0b\u8868\u4e2d\u5b9a\u4e49\u3002\n<\/div>\n\n\n\n\u540d\u79f0<\/td>\n \u503c<\/td>\n<\/tr>\n \n async<\/strong> <\/td>\n\u5b83\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5b83\u7684\u9ed8\u8ba4\u503c\u4e3a true <\/strong>\uff0c\u8fd9\u610f\u5473\u7740\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8bf7\u6c42\u662f\u5f02\u6b65\u5904\u7406\u7684\u3002\u5bf9\u4e8e\u540c\u6b65\u8bf7\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a false <\/strong>\u3002<\/td>\n<\/tr>\n\n beforeSend(xhr)<\/strong> <\/td>\n\u5b83\u662f\u5728\u53d1\u9001\u8bf7\u6c42\u4e4b\u524d\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002<\/td>\n<\/tr>\n \n cache<\/strong> <\/td>\n\u5b83\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5176\u9ed8\u8ba4\u503c\u4e3a true <\/strong>\u3002\u5b83\u8868\u793a\u6d4f\u89c8\u5668\u662f\u5426\u7f13\u5b58\u8bf7\u6c42\u7684\u9875\u9762\u3002\u5c06\u5176\u8bbe\u7f6e\u4e3a false <\/strong>\u65f6\uff0c\u5b83\u5c06\u5f3a\u5236\u9875\u9762\u4e0d\u88ab\u6d4f\u89c8\u5668\u7f13\u5b58\u3002<\/td>\n<\/tr>\n\n complete(xhr, status)<\/strong> <\/td>\n\u662f\u8bf7\u6c42\u5b8c\u6210\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002\u5b83\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f xhr(XMLHttpRequest)<\/em> <\/strong>\u548c \u72b6\u6001<\/em> <\/strong>\u3002 \u72b6\u6001<\/em> <\/strong>\u53ef\u4ee5\u662f\"\u6210\u529f\"\uff0c\"\u672a\u4fee\u6539\"\uff0c\"\u65e0\u5185\u5bb9\"\uff0c\"\u9519\u8bef\"\u7b49\u3002\u5b83\u662f\u8bf7\u6c42\u5b8c\u6210\u540e\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002\u5b83\u5177\u6709\u4e24\u4e2a\u53c2\u6570xhr(XMLHttpRequest)\u548c\u4e00\u4e2a\u72b6\u6001\u3002\u72b6\u6001\u53ef\u4ee5\u662f\"\u6210\u529f\"\uff0c\"\u672a\u4fee\u6539\"\uff0c\"\u65e0\u5185\u5bb9\"\uff0c\"\u9519\u8bef\"\u7b49\u3002<\/td>\n<\/tr>\n\n contentType <\/strong> <\/td>\n\u5728\u5c06\u6570\u636e\u53d1\u9001\u5230\u670d\u52a1\u5668\u65f6\u4f7f\u7528\u3002\u5176\u9ed8\u8ba4\u503c\u4e3a\" application\/x-www-form-urlencoded\"\u3002<\/td>\n<\/tr>\n \n data<\/strong> <\/td>\n\u5b83\u662f\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\uff0c\u6570\u7ec4\u6216JSON\u5bf9\u8c61\u3002<\/td>\n<\/tr>\n \n dataFilter(data, type)<\/strong> <\/td>\n\u8fd9\u662f\u4e00\u4e2a\u5904\u7406XMLHttpRequest\u7684\u539f\u59cb\u54cd\u5e94\u6570\u636e\u7684\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u3002<\/td>\n<\/tr>\n \n dataType<\/strong> <\/td>\n\u8fd9\u662f\u6211\u4eec\u671f\u671b\u670d\u52a1\u5668\u63d0\u4f9b\u7684\u6570\u636e\u7c7b\u578b\u3002<\/td>\n<\/tr>\n \n error(xhr, status, error)<\/strong> <\/td>\n\u8fd9\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u8bf7\u6c42\u5931\u8d25\u65f6\u5c06\u6267\u884c\u8be5\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\u3002<\/td>\n<\/tr>\n \n global<\/strong> <\/td>\n\u5b83\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5176\u9ed8\u8ba4\u503c\u4e3a true <\/strong>\u3002\u5b83\u8868\u793a\u662f\u5426\u89e6\u53d1\u5168\u5c40AJAX\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002<\/td>\n<\/tr>\n\n ifModified <\/strong> <\/td>\n\u5b83\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5176\u9ed8\u8ba4\u503c\u4e3a false <\/strong>\u3002\u5982\u679c\u54cd\u5e94\u81ea\u4e0a\u4e00\u4e2a\u6807\u5934\u4ee5\u6765\u5df2\u53d1\u751f\u66f4\u6539\uff0c\u5b83\u5c06\u6307\u5b9a\u4e00\u4e2a\u6210\u529f\u7684\u8bf7\u6c42\u3002<\/td>\n<\/tr>\n\n jsonp <\/strong> <\/td>\n\u5b83\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b83\u5c06\u8986\u76d6jsonp\u8bf7\u6c42\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u540d\u79f0\u3002<\/td>\n<\/tr>\n \n jsonpCallback <\/strong> <\/td>\n\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542bjsonp\u8bf7\u6c42\u7684\u56de\u8c03\u51fd\u6570\u7684\u540d\u79f0\u3002<\/td>\n<\/tr>\n \n password<\/strong> <\/td>\n\u5b83\u7528\u4e8e\u6307\u5b9aHTTP\u8bbf\u95ee\u8eab\u4efd\u9a8c\u8bc1\u8bf7\u6c42\u4e2d\u4f7f\u7528\u7684\u5bc6\u7801\u3002<\/td>\n<\/tr>\n \n processData <\/strong> <\/td>\n\u5b83\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5176\u9ed8\u8ba4\u503c\u4e3a true <\/strong>\u3002\u7528\u4e8e\u6307\u5b9a\u662f\u5426\u5c06\u4e0e\u8bf7\u6c42\u4e00\u8d77\u53d1\u9001\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u67e5\u8be2\u5b57\u7b26\u4e32\u3002<\/td>\n<\/tr>\n\n scriptCharset <\/strong> <\/td>\n\u5b83\u6307\u5b9a\u8bf7\u6c42\u7684\u5b57\u7b26\u96c6\u3002\u4ec5\u5728\u4f7f\u7528\"\u811a\u672c\"\u4f20\u8f93\u65f6\u9002\u7528\u3002<\/td>\n<\/tr>\n \n success(result, status, xhr)<\/strong> <\/td>\n\u987e\u540d\u601d\u4e49\uff0c\u8be5\u56de\u8c03\u51fd\u6570\u5728\u8bf7\u6c42\u6210\u529f\u65f6\u6267\u884c\u3002\u5b83\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\u3002<\/td>\n<\/tr>\n \n timeout<\/strong> <\/td>\n\u8fd9\u662f\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4(\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d)\u3002\u5982\u679c\u5c06\u5176\u8bbe\u7f6e\u4e3a\u503c 0 <\/strong>\uff0c\u5219\u8868\u793a\u6ca1\u6709\u8d85\u65f6\u3002<\/td>\n<\/tr>\n\n traditional<\/strong> <\/td>\n\u5b83\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5982\u679c\u8981\u4f7f\u7528\u4f20\u7edf\u7684\u53c2\u6570\u5e8f\u5217\u5316\u6837\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a true <\/strong>\u3002<\/td>\n<\/tr>\n\n type<\/strong> <\/td>\n\u5b83\u6307\u5b9ahttp\u8bf7\u6c42\u7684\u7c7b\u578b\uff0c\u4f8b\u5982 POST\uff0cPUT\uff0c<\/strong>\u548c GET <\/strong>\u3002\u9ed8\u8ba4\u503c\u4e3aGET\u3002<\/td>\n<\/tr>\n\n url <\/strong> <\/td>\n\u8fd9\u662f\u8bf7\u6c42\u53d1\u9001\u5230\u7684URL\u3002\u9ed8\u8ba4\u503c\u4e3a\u5f53\u524d\u9875\u9762\u3002<\/td>\n<\/tr>\n \n username<\/strong> <\/td>\n\u8fd9\u662f\u5c06\u5728HTTP\u8bbf\u95ee\u8eab\u4efd\u9a8c\u8bc1\u8bf7\u6c42\u4e2d\u4f7f\u7528\u7684\u7528\u6237\u540d\u3002<\/td>\n<\/tr>\n \n xhr <\/strong> <\/td>\n\u8fd9\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efaXMLHttpRequest\u5bf9\u8c61\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n \u901a\u8fc7\u4e00\u4e9b\u8bf4\u660e\u8ba9\u6211\u4eec\u4e86\u89e3
\n ajax()<\/strong>\u65b9\u6cd5\u7684\u4f7f\u7528\u3002\n<\/div>\n\u793a\u4f8b1 <\/h3>\n\n \u6709\u4e0b\u9762\u7ed9\u51fa\u4e86\u4e00\u4e9b\u6b65\u9aa4\u3002\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff0c\u4ee5\u66f4\u6e05\u695a\u5730\u4e86\u89e3
\n ajax()<\/strong>\u65b9\u6cd5\u7684\u4f7f\u7528\u3002\n<\/div>\n\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u4e0b\u8f7djQuery\u5e93\u6216\u901a\u8fc7\u8bbf\u95ee\u5176\u5b98\u65b9\u7f51\u7ad9\u6765\u67e5\u627ejQuery\u6700\u65b0\u7248\u672c\u7684\u94fe\u63a5\u3002<\/span>
\n\u5176\u6b21\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aHTML\u6587\u6863\uff0c\u5305\u62ecjQuery\u5e93\u3002<\/span>
\n\u6700\u540e\uff0c\u5728 <script><\/strong>\u6807\u7b7e\u5185\u7f16\u5199jQuery\u7684 ready()<\/strong> \u51fd\u6570\uff0c \u5e76\u5728\u5176\u4e3b\u4f53\u4e2d\u7f16\u5199 ajax() <\/strong>\u65b9\u6cd5\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u53c2\u6570\u4f20\u9012\u7ed9 ajax()<\/strong> \u51fd\u6570\u3002 \u6211\u4eec\u5fc5\u987b\u5305\u542bURL\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u5c5e\u6027\u53d1\u9001\u8bf7\u6c42\u3002 \u5728 URL <\/strong>\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3aajax()\u51fd\u6570\u8bbe\u7f6e\u4efb\u4f55\u53ef\u9009\u5c5e\u6027\uff0c\u4f8b\u5982 type\uff0casync\uff0ccache\uff0csuccess\uff0cerror\uff0ccomplete <\/strong> \u7b49\u3002 <\/span> <\/p>\n\n \u73b0\u5728\uff0c \u8ba9\u6211\u4eec\u770b\u770b\u4f7f\u7528
\n ajax() < \/strong>\u65b9\u6cd5\u7684\u4ee3\u7801\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5305\u62ec URL <\/strong> \u53c2\u6570\u548c\u53ef\u9009\u7684 type <\/strong>\u53c2\u6570\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c ajax()<\/strong> \u65b9\u6cd5\u7528\u4e8e\u901a\u8fc7ajax\u8bf7\u6c42\u6dfb\u52a0\u5185\u5bb9\u3002 <\/strong>\n<\/div>\n <\/strong> <\/p>\n\n \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f20\u9012\u4e86 test.html <\/em> <\/strong>\u5230 ajax() <\/strong> \u65b9\u6cd5\u7684 < strong> URL <\/strong>\u53c2\u6570\u3002\n<\/div>\n\n test.html <\/strong>\n<\/div>\n\n <h1><\/span> Hello World <\/h1><\/span>
<h2><\/span> Welcome to the bianchenghao6.com <\/h2><\/span>
<\/span><\/code><\/pre>\n<\/div>\n\n Example2.html <\/strong>\n<\/div>\n\n <!DOCTYPE html>
<html><\/span>
<head><\/span>
<script src=\"\/ajax\/libs\/jquery\/3.5.1\/jquery.min.js\"><\/script><\/span>
<script><\/span>
$(document).ready(function(){
$(\"button\").click(function(){
jQuery.ajax({
url: 'test.html',
type: 'GET',
success: function(data) {
$(\"#para\").html(data);
}
});
});
});
<\/script><\/span>
<\/head><\/span>
<body><\/span>
<h3><\/span> This is an example of using the jQuery's ajax() method. <\/h3><\/span>
<h4><\/span> Click the following button to see the effect. <\/h4><\/span>
<button><\/span> Click me <\/button><\/span>
<p id = \"para\"><\/p><\/span>
<\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/div>\n\n \u8f93\u51fa<\/strong>\n<\/div>\n\n