{"id":32517,"date":"2024-11-25T11:46:45","date_gmt":"2024-11-25T03:46:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32517\/"},"modified":"2024-11-25T11:46:45","modified_gmt":"2024-11-25T03:46:45","slug":"jquery-%e7%ae%80%e5%8c%96%e6%8c%87%e5%8d%97%ef%bc%9a%e6%8e%a2%e7%b4%a2-jquery-%e5%92%8c-ajax","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32517\/","title":{"rendered":"jQuery \u7b80\u5316\u6307\u5357\uff1a\u63a2\u7d22 jQuery \u548c Ajax"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169365990428948.jpg\" class=\"aligncenter\" title=\"jQuery \u7b80\u5316\u6307\u5357\uff1a\u63a2\u7d22 jQuery \u548c Ajax\u63d2\u56fe\" alt=\"jQuery \u7b80\u5316\u6307\u5357\uff1a\u63a2\u7d22 jQuery \u548c Ajax\u63d2\u56fe\" \/><\/p>\n<\/p>\n<h2>jQuery ajax() \u51fd\u6570\u662f\u6700\u4f4e\u7ea7\u522b\u7684\u62bd\u8c61<\/h2>\n<p>jQuery ajax() \u51fd\u6570\u662f\u53ef\u7528\u4e8e XMLHttpRequest\uff08\u53c8\u540d AJAX\uff09\u7684\u6700\u4f4e\u62bd\u8c61\u7ea7\u522b\u3002\u6240\u6709\u5176\u4ed6 jQuery AJAX \u51fd\u6570\uff08\u4f8b\u5982 load()\uff09\u90fd\u5229\u7528 ajax() \u51fd\u6570\u3002\u4f7f\u7528 ajax() \u51fd\u6570\u4e3a XMLHttpRequests \u63d0\u4f9b\u4e86\u6700\u5f3a\u5927\u7684\u529f\u80fd\u3002 jQuery \u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u66f4\u9ad8\u7ea7\u522b\u7684\u62bd\u8c61\u6765\u6267\u884c\u975e\u5e38\u7279\u5b9a\u7c7b\u578b\u7684 XMLHttpRequests\u3002\u8fd9\u4e9b\u51fd\u6570\u672c\u8d28\u4e0a\u662f ajax() \u65b9\u6cd5\u7684\u5feb\u6377\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p>\u8fd9\u4e9b\u5feb\u6377\u65b9\u6cd5\u662f\uff1a<\/p>\n<ul>\n<li>\u52a0\u8f7d()<\/li>\n<li>get()<\/li>\n<li>getJSON()<\/li>\n<li>getScript()<\/li>\n<li>post()<\/li>\n<\/ul>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u867d\u7136\u5feb\u6377\u65b9\u5f0f\u6709\u65f6\u5f88\u597d\uff0c\u4f46\u5b83\u4eec\u90fd\u5728\u5e55\u540e\u4f7f\u7528 ajax() \u3002\u800c\u4e14\uff0c\u5f53\u60a8\u9700\u8981 jQuery \u5728 AJAX \u65b9\u9762\u63d0\u4f9b\u7684\u6240\u6709\u529f\u80fd\u548c\u81ea\u5b9a\u4e49\u529f\u80fd\u65f6\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528 ajax() \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cajax() \u548c load() AJAX \u51fd\u6570\u5747\u4f7f\u7528 GET HTTP \u534f\u8bae\u3002<\/p>\n<hr>\n<h2>jQuery \u652f\u6301\u8de8\u57df JSONP<\/h2>\n<p>\u5e26\u6709\u586b\u5145\u7684 JSON (JSONP) \u662f\u4e00\u79cd\u6280\u672f\uff0c\u5141\u8bb8 HTTP \u8bf7\u6c42\uff08\u8fd4\u56de JSON\uff09\u7684\u53d1\u9001\u8005\u4e3a\u4f7f\u7528 JSON \u5bf9\u8c61\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u8fdb\u884c\u8c03\u7528\u7684\u51fd\u6570\u63d0\u4f9b\u540d\u79f0\u3002\u8be5\u6280\u672f\u4e0d\u4f7f\u7528 XHR\u3002\u5b83\u4f7f\u7528\u811a\u672c\u5143\u7d20\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06\u6570\u636e\u4ece\u4efb\u4f55\u7ad9\u70b9\u62c9\u5165\u4efb\u4f55\u7ad9\u70b9\u3002\u76ee\u7684\u662f\u4e3a\u4e86\u89c4\u907fXMLHttpRequest\u7684\u540c\u6e90\u7b56\u7565\u9650\u5236\u3002<\/p>\n<p>\u4f7f\u7528 getJSON() jQuery \u51fd\u6570\uff0c\u5f53 URL \u4e2d\u6dfb\u52a0 JSONP \u56de\u8c03\u51fd\u6570\u65f6\uff0c\u60a8\u53ef\u4ee5\u4ece\u53e6\u4e00\u4e2a\u57df\u52a0\u8f7d JSON \u6570\u636e\u3002\u4f5c\u4e3a\u793a\u4f8b\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528 Flickr API \u7684 URL \u8bf7\u6c42\u7684\u6837\u5b50\u3002<\/p>\n<p><span>http:\/\/api.flickr.com\/services\/feeds\/photos_public.gne?tags=resig&amp;tagmode=all&amp;format=json&amp;jsoncallback<\/span>=? <\/p>\n<p>? \u503c\u7528\u4f5c\u5feb\u6377\u65b9\u5f0f\uff0c\u544a\u8bc9 jQuery \u8c03\u7528\u4f5c\u4e3a getJSON() \u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u7684\u51fd\u6570\u3002\u5982\u679c\u60a8\u4e0d\u60f3\u4f7f\u7528\u6b64\u5feb\u6377\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06 ? \u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u3002<\/p>\n<p>\u4e0b\u9762\uff0c\u6211\u4f7f\u7528 Flickr JSONP API \u62c9\u5165\u4e00\u4e2a\u7f51\u9875\uff0c\u5176\u4e2d\u5305\u542b\u5e26\u6709\u201cresig\u201d\u6807\u7b7e\u7684\u6700\u65b0\u7167\u7247\u3002\u8bf7\u6ce8\u610f\uff0c\u6211\u4f7f\u7528\u7684\u662f ? \u5feb\u6377\u65b9\u5f0f\uff0c\u56e0\u6b64 jQuery \u5c06\u7b80\u5355\u5730\u8c03\u7528\u6211\u63d0\u4f9b\u7684 getJSON() \u51fd\u6570\u7684\u56de\u8c03\u51fd\u6570\u3002\u4f20\u9012\u7ed9\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\u662f\u8bf7\u6c42\u7684JSON\u6570\u636e\u3002<\/p>\n<pre>\n\n\n\n    &lt;script src=\"http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.2\/jquery.min.js\"&gt;&lt;\/script&gt;&lt;script&gt;  (function($){      \n      $.getJSON(\"http:\/\/api.flickr.com\/services\/feeds\/photos_public.gne?tags=resig&amp;tagmode=all&amp;format=json&amp;jsoncallback=?\",\n          \/\/ Using ? just means call the callback function provided\n          function (data) { \/\/ Data is the JSON object from Flickr    \n              $.each(data.items, function (i, item) { $('&lt;img  \/ alt=\"jQuery \u7b80\u5316\u6307\u5357\uff1a\u63a2\u7d22 jQuery \u548c Ajax\" &gt;').attr(\"src\", item.media.m).appendTo('body'); if (i == 30) return false; });\n          });\n  })(jQuery); &lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u8bf7\u786e\u4fdd\u68c0\u67e5\u60a8\u6240\u4f7f\u7528\u7684\u670d\u52a1\u7684 API \u4ee5\u4fbf\u6b63\u786e\u4f7f\u7528\u56de\u8c03\u3002\u4f8b\u5982\uff0cFlickr \u4f7f\u7528\u540d\u79f0 jsoncallback=?\uff0c\u800c Yahoo!\u548c Digg \u4f7f\u7528\u540d\u79f0 callback=?\u3002<\/p>\n<hr>\n<h2>\u505c\u6b62\u6d4f\u89c8\u5668\u7f13\u5b58 XHR \u8bf7\u6c42<\/h2>\n<p>\u6267\u884c XHR \u8bf7\u6c42\u65f6\uff0cInternet Explorer \u5c06\u7f13\u5b58\u54cd\u5e94\u3002\u5982\u679c\u54cd\u5e94\u5305\u542b\u5177\u6709\u8f83\u957f\u4fdd\u8d28\u671f\u7684\u9759\u6001\u5185\u5bb9\uff0c\u5219\u7f13\u5b58\u53ef\u80fd\u662f\u4e00\u4ef6\u597d\u4e8b\u3002\u4f46\u662f\uff0c\u5982\u679c\u8bf7\u6c42\u7684\u5185\u5bb9\u662f\u52a8\u6001\u7684\u5e76\u4e14\u53ef\u80fd\u968f\u65f6\u53d1\u751f\u53d8\u5316\uff0c\u60a8\u5c06\u9700\u8981\u786e\u4fdd\u6d4f\u89c8\u5668\u4e0d\u4f1a\u7f13\u5b58\u8be5\u8bf7\u6c42\u3002\u4e00\u79cd\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u552f\u4e00\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u503c\u9644\u52a0\u5230 URL \u7684\u672b\u5c3e\u3002\u8fd9\u5c06\u786e\u4fdd\u6d4f\u89c8\u5668\u5bf9\u4e8e\u6bcf\u4e2a\u8bf7\u6c42\u8bf7\u6c42\u552f\u4e00\u7684 URL\u3002<\/p>\n<pre>\n\/\/ Add unique query string at end of the URL\nurl: 'some?nocache='+(newDate()).getTime()\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u53e6\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\uff08\u66f4\u591a\u7684\u662f\u5168\u5c40\u89e3\u51b3\u65b9\u6848\uff09\u662f\u9ed8\u8ba4\u8bbe\u7f6e\u6240\u6709 AJAX \u8bf7\u6c42\u4ee5\u5305\u542b\u6211\u4eec\u521a\u624d\u8ba8\u8bba\u7684\u65e0\u7f13\u5b58\u903b\u8f91\u3002\u4e3a\u6b64\uff0c\u8bf7\u4f7f\u7528 ajaxSetup \u51fd\u6570\u5168\u5c40\u5173\u95ed\u7f13\u5b58\u3002<\/p>\n<pre>\n$.ajaxSetup({\n            cache: false \/\/ True by default. False means caching is off.\n});\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u4e3a\u4e86\u7528\u5355\u72ec\u7684 XHR \u8bf7\u6c42\u8986\u76d6\u6b64\u5168\u5c40\u8bbe\u7f6e\uff0c\u60a8\u53ea\u9700\u5728\u4f7f\u7528 ajax() \u51fd\u6570\u65f6\u66f4\u6539\u7f13\u5b58\u9009\u9879\u5373\u53ef\u3002\u4e0b\u9762\u662f\u4f7f\u7528 ajax() \u51fd\u6570\u6267\u884c XHR \u8bf7\u6c42\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u8be5\u51fd\u6570\u5c06\u8986\u76d6\u5168\u5c40\u8bbe\u7f6e\u5e76\u7f13\u5b58\u8bf7\u6c42\u3002<\/p>\n<pre>\n$.ajaxSetup ({   \n    cache: false \/\/ True by default. False means caching is off.   \n});       \njQuery.ajax({ cache: true, url: 'some', type: 'POST' } );\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjQuery \u7b80\u5316\u6307\u5357\uff1a\u63a2\u7d22 jQuery \u548c Ajax\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery ajax() \u51fd\u6570\u662f\u6700\u4f4e\u7ea7\u522b\u7684\u62bd\u8c61 jQuery ajax() \u51fd\u6570\u662f\u53ef\u7528\u4e8e XMLHttpRequest\uff08\u53c8\u540d AJAX\uff09\u7684\u6700\u4f4e\u62bd\u8c61\u7ea7\u522b\u3002\u6240\u6709\u5176\u4ed6 jQuery AJAX \u51fd\u6570\uff08\u4f8b\u5982 load()\uff09\u90fd\u5229\u7528 ajax() \u51fd\u6570\u3002\u4f7f\u7528 ajax() \u51fd\u6570\u4e3a XMLHttpRequests \u63d0\u4f9b\u4e86\u6700\u5f3a\u5927\u7684\u529f\u80fd\u3002 jQuery \u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u66f4\u9ad8\u7ea7\u522b\u7684\u62bd\u8c61\u6765\u6267\u884c\u975e\u5e38\u7279\u5b9a\u7c7b\u578b\u7684 XMLHttpRequests\u3002\u8fd9\u4e9b\u51fd\u6570\u672c\u8d28\u4e0a\u662f ajax() \u65b9\u6cd5\u7684\u5feb\u6377\u65b9\u5f0f\u3002 \u8fd9\u4e9b\u5feb\u6377\u65b9\u6cd5\u662f\uff1a \u52a0\u8f7d() get() getJSON() getScript() post() \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u867d\u7136\u5feb\u6377\u65b9\u5f0f\u6709\u65f6\u5f88\u597d\uff0c\u4f46\u5b83\u4eec\u90fd\u5728\u5e55\u540e\u4f7f\u7528 ajax() \u3002\u800c\u4e14\uff0c\u5f53\u60a8\u9700\u8981 jQuery \u5728 AJAX \u65b9\u9762\u63d0\u4f9b\u7684\u6240\u6709\u529f\u80fd\u548c\u81ea\u5b9a\u4e49\u529f\u80fd\u65f6\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528 ajax() \u65b9\u6cd5\u3002 \u6ce8\u610f\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cajax() \u548c load() AJAX \u51fd\u6570\u5747\u4f7f\u7528 GET HTTP \u534f\u8bae\u3002 jQuery \u652f\u6301\u8de8\u57df JSONP \u5e26\u6709\u586b\u5145\u7684 JSON (JSONP) \u662f\u4e00\u79cd\u6280\u672f\uff0c\u5141\u8bb8 HTTP \u8bf7\u6c42\uff08\u8fd4\u56de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32517","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32517"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32517\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}