
{"id":2513,"date":"2020-11-23T15:13:34","date_gmt":"2020-11-23T07:13:34","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2513"},"modified":"2020-11-23T16:36:57","modified_gmt":"2020-11-23T08:36:57","slug":"2020javaweb%e6%95%99%e7%a8%8b%e4%b9%8bajax%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2513","title":{"rendered":"2020javaweb\u6559\u7a0b\u4e4bajax\u65b9\u6cd5"},"content":{"rendered":"<h3>2020javaweb\u6559\u7a0b\u4e4bajax\u65b9\u6cd5<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u5e38\u7528\u53c2\u6570\uff1a<\/strong><\/span><\/p>\n<p>contentType:(\u9ed8\u8ba4: &#8220;application\/x-www-form-urlencoded&#8221;) \u53d1\u9001\u4fe1\u606f\u81f3\u670d\u52a1\u5668\u65f6\u5185\u5bb9\u7f16\u7801\u7c7b\u578b\u3002\u9ed8\u8ba4\u503c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<p>data:\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u6570\u636e<\/p>\n<p>dataType:\u9884\u671f\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u3002\u5982\u679c\u4e0d\u6307\u5b9a\uff0cjQuery \u5c06\u81ea\u52a8\u6839\u636e HTTP \u5305 MIME \u4fe1\u606f\u6765\u667a\u80fd\u5224\u65ad\u3002<\/p>\n<p>&#8220;xml&#8221;: \u8fd4\u56de XML \u6587\u6863\uff0c\u53ef\u7528 jQuery \u5904\u7406\u3002<\/p>\n<p>&#8220;html&#8221;: \u8fd4\u56de\u7eaf\u6587\u672c HTML \u4fe1\u606f\uff1b\u5305\u542b\u7684script\u6807\u7b7e\u4f1a\u5728\u63d2\u5165dom\u65f6\u6267\u884c\u3002<\/p>\n<p>&#8220;script&#8221;: \u8fd4\u56de\u7eaf\u6587\u672c JavaScript \u4ee3\u7801\u3002\u4e0d\u4f1a\u81ea\u52a8\u7f13\u5b58\u7ed3\u679c\u3002\u9664\u975e\u8bbe\u7f6e\u4e86&#8221;cache&#8221;\u53c2\u6570\u3002&#8221;&#8217;\u6ce8\u610f\uff1a&#8221;&#8217;\u5728\u8fdc\u7a0b\u8bf7\u6c42\u65f6(\u4e0d\u5728\u540c\u4e00\u4e2a\u57df\u4e0b)\uff0c\u6240\u6709POST\u8bf7\u6c42\u90fd\u5c06\u8f6c\u4e3aGET\u8bf7\u6c42\u3002(\u56e0\u4e3a\u5c06\u4f7f\u7528DOM\u7684script\u6807\u7b7e\u6765\u52a0\u8f7d)<\/p>\n<p>&#8220;json&#8221;: \u8fd4\u56de JSON \u6570\u636e \u3002<!--more--><\/p>\n<p>&#8220;jsonp&#8221;: JSONP \u683c\u5f0f\u3002\u4f7f\u7528 JSONP \u5f62\u5f0f\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5982 &#8220;myurl?callback=?&#8221; jQuery \u5c06\u81ea\u52a8\u66ff\u6362 ? \u4e3a\u6b63\u786e\u7684\u51fd\u6570\u540d\uff0c\u4ee5\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>&#8220;text&#8221;: \u8fd4\u56de\u7eaf\u6587\u672c\u5b57\u7b26\u4e32<\/p>\n<p>success\uff1a\u8bf7\u6c42\u6210\u529f\u540e\u7684\u56de\u8c03\u51fd\u6570\u3002\u53c2\u6570\uff1a\u7531\u670d\u52a1\u5668\u8fd4\u56de\uff0c\u5e76\u6839\u636edataType\u53c2\u6570\u8fdb\u884c\u5904\u7406\u540e\u7684\u6570\u636e\uff1b\u63cf\u8ff0\u72b6\u6001\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>type\uff1a(\u9ed8\u8ba4: &#8220;GET&#8221;) \u8bf7\u6c42\u65b9\u5f0f (&#8220;POST&#8221; \u6216 &#8220;GET&#8221;)\uff0c \u9ed8\u8ba4\u4e3a &#8220;GET&#8221;\u3002\u6ce8\u610f\uff1a\u5176\u5b83 HTTP \u8bf7\u6c42\u65b9\u6cd5\uff0c\u5982 PUT \u548c DELETE \u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u4f46\u4ec5\u90e8\u5206\u6d4f\u89c8\u5668\u652f\u6301\u3002<\/p>\n<p>url\uff1a(\u9ed8\u8ba4: \u5f53\u524d\u9875\u5730\u5740) \u53d1\u9001\u8bf7\u6c42\u7684\u5730\u5740\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>&lt;script&gt;\r\n    function checks() {\r\n        var values=$(\"#username\").val();\r\n        $.ajax({\r\n            \/\/\u8bf7\u6c42\u65b9\u5f0f\r\n            type:\"get\",\r\n            \/\/\u8bf7\u6c42\u7684\u5730\u5740\uff0c\u4e0d\u7528\u5199\u9879\u76ee\u540d\r\n            url:\"check\",\r\n            \/\/\u53d1\u9001\u6570\u636e get\u53ef\u4ee5\u5728url\u76f4\u63a5\u62fc\u63a5\r\n            data:\"username=\"+values,\r\n            \/\/\u670d\u52a1\u5668\u8fd4\u56de\u7684\u7c7b\u578b\uff0c\u76f4\u63a5\u5bf9\u5176\u8fdb\u884c\u89e3\u6790\r\n            dataType:\"text\",\r\n            \/\/\u6210\u529f\u4e4b\u540e\u56de\u8c03\u51fd\u6570\r\n            success:function (data) {\r\n                if(data==\"1\"){\r\n                    $(\"#ch\")\r\n                        .html(values+\"\u5df2\u5b58\u5728\uff01\u8bf7\u91cd\u65b0\u8f93\u5165\")\r\n                        .css(\"color\",\"red\");\r\n                }else{\r\n                    $(\"#ch\")\r\n                        .html(values+\"\u53ef\u4ee5\u6ce8\u518c\")\r\n                        .css(\"color\",\"green\");\r\n                }\r\n            }\r\n        })\r\n    }\r\n&lt;\/script&gt;<\/pre>\n<p><strong>get\u65b9\u6cd5<\/strong><\/p>\n<pre>function checks(){\r\n    var values=$(\"#username\").val();\r\n    \/\/<strong>$.get(url,data,success,dataType)<\/strong>\r\n    $.get(\"check\",\"username=\"+values,function(data){\r\n        if(data==\"1\"){\r\n            $(\"#ch\").html(values+\"\u7528\u6237\u540d\u88ab\u5360\u7528\").css(\"color\",\"red\");\r\n        }else{\r\n            $(\"#ch\").html(values+\"\u53ef\u4ee5\u6ce8\u518c\").css(\"color\",\"green\");\r\n        }\r\n    },\"json\")\r\n}<\/pre>\n<p><strong>post\u65b9\u6cd5<\/strong><\/p>\n<pre>function checks() {\r\n    var values=$(\"#username\").val();\r\n    \/\/\u67e5\u8be2 \u7528 get\r\n    \/\/\u589e\u5220\u6539 \u7528 post\r\n    $.post(\"check\",\"username=\"+values,function(v){\r\n        if(v==\"1\"){\r\n            $(\"#ch\").html(values+\"\u7528\u6237\u540d\u88ab\u5360\u7528\").css(\"color\",\"red\");\r\n        }else{\r\n            $(\"#ch\").html(values+\"\u53ef\u4ee5\u6ce8\u518c\").css(\"color\",\"green\");\r\n        }\r\n    })\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2020javaweb\u6559\u7a0b\u4e4bajax\u65b9\u6cd5 \u5e38\u7528\u53c2\u6570\uff1a contentType:(\u9ed8\u8ba4: &#8220;appl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2513","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2513"}],"version-history":[{"count":3,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2513\/revisions"}],"predecessor-version":[{"id":2516,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2513\/revisions\/2516"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}