{"id":523,"date":"2014-07-29T07:19:36","date_gmt":"2014-07-28T23:19:36","guid":{"rendered":"http:\/\/www.shuran.cn\/?p=523"},"modified":"2014-07-29T07:19:36","modified_gmt":"2014-07-28T23:19:36","slug":"jquery%e4%b8%ad-ajax%e6%96%b9%e6%b3%95%e5%8f%82%e6%95%b0%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.shuran.cn\/?p=523","title":{"rendered":"JQuery\u4e2d$.ajax()\u65b9\u6cd5\u53c2\u6570\u8be6\u89e3"},"content":{"rendered":"<p>\n\turl: \u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\uff08\u9ed8\u8ba4\u4e3a\u5f53\u524d\u9875\u5730\u5740\uff09\u53d1\u9001\u8bf7\u6c42\u7684\u5730\u5740\u3002\n<\/p>\n<p>\n\ttype: \u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8bf7\u6c42\u65b9\u5f0f\uff08post\u6216get\uff09\u9ed8\u8ba4\u4e3aget\u3002\u6ce8\u610f\u5176\u4ed6http\u8bf7\u6c42\u65b9\u6cd5\uff0c\u4f8b\u5982put\u548c\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp;delete\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u4f46\u4ec5\u90e8\u5206\u6d4f\u89c8\u5668\u652f\u6301\u3002\n<\/p>\n<p>\n\ttimeout: \u8981\u6c42\u4e3aNumber\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\uff08\u6beb\u79d2\uff09\u3002\u6b64\u8bbe\u7f6e\u5c06\u8986\u76d6$.ajaxSetup()\u65b9\u6cd5\u7684\u5168\u5c40\u8bbe\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; \u7f6e\u3002\n<\/p>\n<p>\n\tasync\uff1a\u8981\u6c42\u4e3aBoolean\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u9ed8\u8ba4\u8bbe\u7f6e\u4e3atrue\uff0c\u6240\u6709\u8bf7\u6c42\u5747\u4e3a\u5f02\u6b65\u8bf7\u6c42\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; \u5982\u679c\u9700\u8981\u53d1\u9001\u540c\u6b65\u8bf7\u6c42\uff0c\u8bf7\u5c06\u6b64\u9009\u9879\u8bbe\u7f6e\u4e3afalse\u3002\u6ce8\u610f\uff0c\u540c\u6b65\u8bf7\u6c42\u5c06\u9501\u4f4f\u6d4f\u89c8\u5668\uff0c\u7528\u6237\u5176\u4ed6\u64cd\u4f5c\u5fc5\u987b\u7b49\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; \u5f85\u8bf7\u6c42\u5b8c\u6210\u624d\u53ef\u4ee5\u6267\u884c\u3002\n<\/p>\n<p>\n\tcache\uff1a\u8981\u6c42\u4e3aBoolean\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3atrue\uff08\u5f53dataType\u4e3ascript\u65f6\uff0c\u9ed8\u8ba4\u4e3afalse\uff09\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; \u8bbe\u7f6e\u4e3afalse\u5c06\u4e0d\u4f1a\u4ece\u6d4f\u89c8\u5668\u7f13\u5b58\u4e2d\u52a0\u8f7d\u8bf7\u6c42\u4fe1\u606f\u3002\n<\/p>\n<p>\n\tdata: \u8981\u6c42\u4e3aObject\u6216String\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u6570\u636e\u3002\u5982\u679c\u5df2\u7ecf\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\u5c06\u81ea\u52a8\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp;\u5f0f\u3002get\u8bf7\u6c42\u4e2d\u5c06\u9644\u52a0\u5728url\u540e\u3002\u9632\u6b62\u8fd9\u79cd\u81ea\u52a8\u8f6c\u6362\uff0c\u53ef\u4ee5\u67e5\u770bprocessData\u9009\u9879\u3002\u5bf9\u8c61\u5fc5\u987b\u4e3akey\/value\u683c\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp;\u5f0f\uff0c\u4f8b\u5982{foo1:\"bar1\",foo2:\"bar2\"}\u8f6c\u6362\u4e3a&amp;foo1=bar1&amp;foo2=bar2\u3002\u5982\u679c\u662f\u6570\u7ec4\uff0cJQuery\u5c06\u81ea\u52a8\u4e3a\u4e0d\u540c\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp;\u503c\u5bf9\u5e94\u540c\u4e00\u4e2a\u540d\u79f0\u3002\u4f8b\u5982{foo:[\"bar1\",\"bar2\"]}\u8f6c\u6362\u4e3a&amp;foo=bar1&amp;foo=bar2\u3002\n<\/p>\n<p>\n\tdataType: \u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u9884\u671f\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u3002\u5982\u679c\u4e0d\u6307\u5b9a\uff0cJQuery\u5c06\u81ea\u52a8\u6839\u636ehttp\u5305mime\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u4fe1\u606f\u8fd4\u56deresponseXML\u6216responseText\uff0c\u5e76\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u53c2\u6570\u4f20\u9012\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u53ef\u7528\u7684\u7c7b\u578b\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xml\uff1a\u8fd4\u56deXML\u6587\u6863\uff0c\u53ef\u7528JQuery\u5904\u7406\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;html\uff1a\u8fd4\u56de\u7eaf\u6587\u672cHTML\u4fe1\u606f\uff1b\u5305\u542b\u7684script\u6807\u7b7e\u4f1a\u5728\u63d2\u5165DOM\u65f6\u6267\u884c\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;script\uff1a\u8fd4\u56de\u7eaf\u6587\u672cJavaScript\u4ee3\u7801\u3002\u4e0d\u4f1a\u81ea\u52a8\u7f13\u5b58\u7ed3\u679c\u3002\u9664\u975e\u8bbe\u7f6e\u4e86cache\u53c2\u6570\u3002\u6ce8\u610f\u5728\u8fdc\u7a0b\u8bf7\u6c42\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u65f6\uff08\u4e0d\u5728\u540c\u4e00\u4e2a\u57df\u4e0b\uff09\uff0c\u6240\u6709post\u8bf7\u6c42\u90fd\u5c06\u8f6c\u4e3aget\u8bf7\u6c42\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;json\uff1a\u8fd4\u56deJSON\u6570\u636e\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jsonp\uff1aJSONP\u683c\u5f0f\u3002\u4f7f\u7528SONP\u5f62\u5f0f\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4f8b\u5982myurl?callback=?\uff0cJQuery\u5c06\u81ea\u52a8\u66ff\u6362\u540e\u4e00\u4e2a\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u201c?\u201d\u4e3a\u6b63\u786e\u7684\u51fd\u6570\u540d\uff0c\u4ee5\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text\uff1a\u8fd4\u56de\u7eaf\u6587\u672c\u5b57\u7b26\u4e32\u3002\n<\/p>\n<p>\n\tbeforeSend\uff1a\u8981\u6c42\u4e3aFunction\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u53d1\u9001\u8bf7\u6c42\u524d\u53ef\u4ee5\u4fee\u6539XMLHttpRequest\u5bf9\u8c61\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u6dfb\u52a0\u81ea\u5b9a\u4e49\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HTTP\u5934\u3002\u5728beforeSend\u4e2d\u5982\u679c\u8fd4\u56defalse\u53ef\u4ee5\u53d6\u6d88\u672c\u6b21ajax\u8bf7\u6c42\u3002XMLHttpRequest\u5bf9\u8c61\u662f\u60df\u4e00\u7684\u53c2\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u6570\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function(XMLHttpRequest){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this; &nbsp; \/\/\u8c03\u7528\u672c\u6b21ajax\u8bf7\u6c42\u65f6\u4f20\u9012\u7684options\u53c2\u6570\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}\n<\/p>\n<p>\n\tcomplete\uff1a\u8981\u6c42\u4e3aFunction\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8bf7\u6c42\u5b8c\u6210\u540e\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\uff08\u8bf7\u6c42\u6210\u529f\u6216\u5931\u8d25\u65f6\u5747\u8c03\u7528\uff09\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u53c2\u6570\uff1aXMLHttpRequest\u5bf9\u8c61\u548c\u4e00\u4e2a\u63cf\u8ff0\u6210\u529f\u8bf7\u6c42\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function(XMLHttpRequest, textStatus){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this; &nbsp; &nbsp;\/\/\u8c03\u7528\u672c\u6b21ajax\u8bf7\u6c42\u65f6\u4f20\u9012\u7684options\u53c2\u6570\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}\n<\/p>\n<p>\n\tsuccess\uff1a\u8981\u6c42\u4e3aFunction\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8bf7\u6c42\u6210\u529f\u540e\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\uff0c\u6709\u4e24\u4e2a\u53c2\u6570\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; (1)\u7531\u670d\u52a1\u5668\u8fd4\u56de\uff0c\u5e76\u6839\u636edataType\u53c2\u6570\u8fdb\u884c\u5904\u7406\u540e\u7684\u6570\u636e\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; (2)\u63cf\u8ff0\u72b6\u6001\u7684\u5b57\u7b26\u4e32\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; function(data, textStatus){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/data\u53ef\u80fd\u662fxmlDoc\u3001jsonObj\u3001html\u3001text\u7b49\u7b49\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this; &nbsp;\/\/\u8c03\u7528\u672c\u6b21ajax\u8bf7\u6c42\u65f6\u4f20\u9012\u7684options\u53c2\u6570\n<\/p>\n<p>\n\terror\uff1a\u8981\u6c42\u4e3aFunction\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8bf7\u6c42\u5931\u8d25\u65f6\u88ab\u8c03\u7528\u7684\u51fd\u6570\u3002\u8be5\u51fd\u6570\u67093\u4e2a\u53c2\u6570\uff0c\u5373XMLHttpRequest\u5bf9\u8c61\u3001\u9519\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; \u8bef\u4fe1\u606f\u3001\u6355\u83b7\u7684\u9519\u8bef\u5bf9\u8c61(\u53ef\u9009)\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; ajax\u4e8b\u4ef6\u51fd\u6570\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; function(XMLHttpRequest, textStatus, errorThrown){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u901a\u5e38\u60c5\u51b5\u4e0btextStatus\u548cerrorThrown\u53ea\u6709\u5176\u4e2d\u4e00\u4e2a\u5305\u542b\u4fe1\u606f\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this; &nbsp; \/\/\u8c03\u7528\u672c\u6b21ajax\u8bf7\u6c42\u65f6\u4f20\u9012\u7684options\u53c2\u6570\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; }\n<\/p>\n<p>\n\tcontentType\uff1a\u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5f53\u53d1\u9001\u4fe1\u606f\u81f3\u670d\u52a1\u5668\u65f6\uff0c\u5185\u5bb9\u7f16\u7801\u7c7b\u578b\u9ed8\u8ba4\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u4e3a\"application\/x-www-form-urlencoded\"\u3002\u8be5\u9ed8\u8ba4\u503c\u9002\u5408\u5927\u591a\u6570\u5e94\u7528\u573a\u5408\u3002\n<\/p>\n<p>\n\tdataFilter\uff1a\u8981\u6c42\u4e3aFunction\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u7ed9Ajax\u8fd4\u56de\u7684\u539f\u59cb\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u7684\u51fd\u6570\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u63d0\u4f9bdata\u548ctype\u4e24\u4e2a\u53c2\u6570\u3002data\u662fAjax\u8fd4\u56de\u7684\u539f\u59cb\u6570\u636e\uff0ctype\u662f\u8c03\u7528jQuery.ajax\u65f6\u63d0\u4f9b\u7684\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataType\u53c2\u6570\u3002\u51fd\u6570\u8fd4\u56de\u7684\u503c\u5c06\u7531jQuery\u8fdb\u4e00\u6b65\u5904\u7406\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function(data, type){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8fd4\u56de\u5904\u7406\u540e\u7684\u6570\u636e\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return data;\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}\n<\/p>\n<p>\n\tglobal\uff1a\u8981\u6c42\u4e3aBoolean\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\u8868\u793a\u662f\u5426\u89e6\u53d1\u5168\u5c40ajax\u4e8b\u4ef6\u3002\u8bbe\u7f6e\u4e3afalse\u5c06\u4e0d\u4f1a\u89e6\u53d1\u5168\u5c40\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp;ajax\u4e8b\u4ef6\uff0cajaxStart\u6216ajaxStop\u53ef\u7528\u4e8e\u63a7\u5236\u5404\u79cdajax\u4e8b\u4ef6\u3002\n<\/p>\n<p>\n\tifModified\uff1a\u8981\u6c42\u4e3aBoolean\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3afalse\u3002\u4ec5\u5728\u670d\u52a1\u5668\u6570\u636e\u6539\u53d8\u65f6\u83b7\u53d6\u65b0\u6570\u636e\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u670d\u52a1\u5668\u6570\u636e\u6539\u53d8\u5224\u65ad\u7684\u4f9d\u636e\u662fLast-Modified\u5934\u4fe1\u606f\u3002\u9ed8\u8ba4\u503c\u662ffalse\uff0c\u5373\u5ffd\u7565\u5934\u4fe1\u606f\u3002\n<\/p>\n<p>\n\tjsonp\uff1a\u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5728\u4e00\u4e2ajsonp\u8bf7\u6c42\u4e2d\u91cd\u5199\u56de\u8c03\u51fd\u6570\u7684\u540d\u5b57\u3002\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; \u8be5\u503c\u7528\u6765\u66ff\u4ee3\u5728\"callback=?\"\u8fd9\u79cdGET\u6216POST\u8bf7\u6c42\u4e2dURL\u53c2\u6570\u91cc\u7684\"callback\"\u90e8\u5206\uff0c\u4f8b\u5982\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; {jsonp:'onJsonPLoad'}\u4f1a\u5bfc\u81f4\u5c06\"onJsonPLoad=?\"\u4f20\u7ed9\u670d\u52a1\u5668\u3002\n<\/p>\n<p>\n\tusername\uff1a\u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u7528\u4e8e\u54cd\u5e94HTTP\u8bbf\u95ee\u8ba4\u8bc1\u8bf7\u6c42\u7684\u7528\u6237\u540d\u3002\n<\/p>\n<p>\n\tpassword\uff1a\u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u7528\u4e8e\u54cd\u5e94HTTP\u8bbf\u95ee\u8ba4\u8bc1\u8bf7\u6c42\u7684\u5bc6\u7801\u3002\n<\/p>\n<p>\n\tprocessData\uff1a\u8981\u6c42\u4e3aBoolean\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3atrue\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53d1\u9001\u7684\u6570\u636e\u5c06\u88ab\u8f6c\u6362\u4e3a\u5bf9\u8c61\uff08\u4ece\u6280\u672f\u89d2\u5ea6\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u6765\u8bb2\u5e76\u975e\u5b57\u7b26\u4e32\uff09\u4ee5\u914d\u5408\u9ed8\u8ba4\u5185\u5bb9\u7c7b\u578b\"application\/x-www-form-urlencoded\"\u3002\u5982\u679c\u8981\u53d1\u9001DOM\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u6811\u4fe1\u606f\u6216\u8005\u5176\u4ed6\u4e0d\u5e0c\u671b\u8f6c\u6362\u7684\u4fe1\u606f\uff0c\u8bf7\u8bbe\u7f6e\u4e3afalse\u3002\n<\/p>\n<p>\n\tscriptCharset\uff1a\u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u53ea\u6709\u5f53\u8bf7\u6c42\u65f6dataType\u4e3a\"jsonp\"\u6216\u8005\"script\"\uff0c\u5e76\u4e14type\u662fGET\u65f6\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u624d\u4f1a\u7528\u4e8e\u5f3a\u5236\u4fee\u6539\u5b57\u7b26\u96c6(charset)\u3002\u901a\u5e38\u5728\u672c\u5730\u548c\u8fdc\u7a0b\u7684\u5185\u5bb9\u7f16\u7801\u4e0d\u540c\u65f6\u4f7f\u7528\u3002\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t\u6848\u4f8b\u4ee3\u7801\uff1a\n<\/p>\n<p>\n\t$(function(){\n<\/p>\n<p>\n\t&nbsp; &nbsp;$('#send').click(function(){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: \"GET\",\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: \"test.json\",\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {username:$(\"#username\").val(), content:$(\"#content\").val()},\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: \"json\",\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(data){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#resText').empty(); &nbsp; \/\/\u6e05\u7a7aresText\u91cc\u9762\u7684\u6240\u6709\u5185\u5bb9\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var html = '';\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(data, function(commentIndex, comment){\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html += '&lt;div class=\"comment\"&gt;&lt;h6&gt;' + comment['username']\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + ':&lt;\/h6&gt;&lt;p class=\"para\"' + comment['content']\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + '&lt;\/p&gt;&lt;\/div&gt;';\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#resText').html(html);\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}\n<\/p>\n<p>\n\t&nbsp; &nbsp; &nbsp; &nbsp; });\n<\/p>\n<p>\n\t&nbsp; &nbsp;});\n<\/p>\n<p>\n\t});\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t\u987a\u4fbf\u8bf4\u4e00\u4e0b$.each()\u51fd\u6570:\n<\/p>\n<p>\n\t$.each()\u51fd\u6570\u4e0d\u540c\u4e8eJQuery\u5bf9\u8c61\u7684each()\u65b9\u6cd5\uff0c\u5b83\u662f\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u4e0d\u64cd\u4f5cJQuery\u5bf9\u8c61\uff0c\u800c\u662f\u4ee5\u4e00\u4e2a\u6570\u7ec4\u6216\u8005\u5bf9\u8c61\u4f5c\u4e3a\u7b2c1\u4e2a\u53c2\u6570\uff0c\u4ee5\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u7b2c2\u4e2a\u53c2\u6570\u3002\u56de\u8c03\u51fd\u6570\u62e5\u6709\u4e24\u4e2a\u53c2\u6570\uff1a\u7b2c1\u4e2a\u4e3a\u5bf9\u8c61\u7684\u6210\u5458\u6216\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u7b2c2\u4e2a\u4e3a\u5bf9\u5e94\u53d8\u91cf\u6216\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>url: \u8981\u6c42\u4e3aString\u7c7b\u578b\u7684\u53c2\u6570\uff0c\uff08\u9ed8\u8ba4\u4e3a\u5f53\u524d\u9875\u5730\u5740\uff09\u53d1\u9001\u8bf7\u6c42\u7684\u5730\u5740\u3002 type: \u8981\u6c42\u4e3aString\u7c7b\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.shuran.cn\/?p=523\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-523","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=523"}],"version-history":[{"count":1,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/523\/revisions"}],"predecessor-version":[{"id":524,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/523\/revisions\/524"}],"wp:attachment":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}