{"id":43173,"date":"2024-12-01T14:01:07","date_gmt":"2024-12-01T06:01:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43173\/"},"modified":"2024-12-01T14:01:07","modified_gmt":"2024-12-01T06:01:07","slug":"ajax-%e9%87%8d%e5%ae%9a%e5%90%91%ef%bc%8chttp-statusok-%e7%94%b1-ajax-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b8%94%e6%9c%aa%e9%87%8d%e5%ae%9a%e5%90%91","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43173\/","title":{"rendered":"Ajax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Ajax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411<\/span><\/p>\n<h1>Ajax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 12:30:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aAjax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5728 go \u4e2d\u6709 func\uff08\u6211\u5df2\u7ecf\u5220\u9664\u4e86\u6240\u6709\u9519\u8bef\u5904\u7406\u4ee5\u4f7f\u5176\u7b80\u77ed\uff09<\/p>\n<pre>func postloginajax(w http.responsewriter, r *http.request) {\n    if r.method == \"post\" {\n        useremail := r.formvalue(\"email\")\n        userpassword := r.formvalue(\"password\")\n        db := r.context().value( \"db\").(*sqlx.db)\n        sessionstore := r.context().value( \"sessionstore\").(sessions.store)\n        u := models.newuser(db)\n        user, err := u.getuserbyemailandpassword(nil, useremail, userpassword)\n        session, _ := sessionstore.get(r, \"superadmin-session\")\n        session.values[\"user\"] = user\n        err = session.save(r, w)\n        log.info(\"all success, redirecting to: \", \"\/\")\n        w.header().set(\"location\", \"\/\")\n        w.writeheader(http.statusfound)\n    }\n}<\/pre>\n<p>\u4e0b\u9762\u662f\u6b63\u5728\u4f7f\u7528\u7684\u6a21\u677f\u7684\u7247\u6bb5<\/p>\n<pre>&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.4.1\/jquery.min.js\"&gt;&lt;\/script&gt;\n&lt;script&gt;\n        $(document).ready(function(){\n            $(\"#login-submit\").on('click', function(){\n                $.ajaxsetup({async: true, cache: false});\n                $.ajax({\n                    url: '\/login', \/\/ url where to submit the request\n                    type : \"post\", \/\/ type of action post || get\n                    data : $(\"#form-login\").serialize(), \/\/ post data || get data\n                    success: function (response, status, xhr) {\n                        console.log(\"success\");\n                        console.log(status);\n                        console.log(xhr);\n                        console.log(response);\n                        },\n                    error: function (errmsg) {\n                        console.log(\"error\");\n                        console.log(errmsg);\n                        $(\"#formerrormessage\").html(errmsg);\n                    },\n                    complete: function(resp) {\n                        console.log(\"complete\");\n                        console.log(resp);\n                    }\n                })\n            });\n        });\n&lt;\/script&gt;<\/pre>\n<p>\u5982\u679c\u6211\u53ea\u662f\u4e0a\u9762\u663e\u793a\u7684\u4ee3\u7801\uff0c\u5f53\u6211\u5355\u51fb\u201c\u767b\u5f55\u201d\u6309\u94ae\u5e76\u68c0\u67e5\u63a7\u5236\u53f0\u65e5\u5fd7\u65f6\uff0c\u6211\u4f1a\u770b\u5230\u5982\u4e0b\u6240\u793a\u7684\u5185\u5bb9<\/p>\n<p>\u6211\u4e0d\u660e\u767d\u7684\u662f\u4e3a\u4ec0\u4e48\u9875\u9762\u6ca1\u6709\u91cd\u5b9a\u5411\uff0c\u56e0\u4e3a\u5728\u5f00\u53d1\u5de5\u5177\u7684\u201c\u7f51\u7edc\u201d\u9009\u9879\u5361\u4e2d\u6211\u770b\u5230\u72b6\u6001\u4e3a 302\uff0c\u800c\u5728\u63a7\u5236\u53f0\u65e5\u5fd7\u4e2d\u6211\u770b\u5230\u72b6\u6001\u4e3a 200\u3002\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u8fd9\u79cd\u5dee\u5f02\u3002\u5f53\u6211\u53ea\u662f\u5237\u65b0\u9875\u9762\u65f6\uff0c\u5b83\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u5e76\u5e26\u6211\u5230\u767b\u5f55\u9875\u9762\uff0c\u4ece\u90a3\u91cc\u6211\u5fc5\u987b\u6ce8\u9500\u624d\u80fd\u518d\u6b21\u6d4b\u8bd5\u3002<\/p>\n<p>\u6211\u600e\u6837\u624d\u80fd\u771f\u6b63\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\uff0c\u6bd4\u5982\u4f7f\u7528ajax\u63d0\u4ea4\u8868\u5355\uff0c\u7136\u540e\u5728\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u548c\u5bc6\u7801\u65f6\u91cd\u5b9a\u5411\u3002<\/p>\n<p>\u5982\u679c\u6211\u4ece\u51fd\u6570\u4e2d\u5220\u9664\u4ee5\u4e0b\u884c\uff0c\u54cd\u5e94\u5c06\u7531 ajax \u7684\u9519\u8bef\u90e8\u5206\u5904\u7406\uff0c\u5e76\u4e14\u4f1a\u53d1\u751f\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u6211\u5728\u63a7\u5236\u53f0\u65e5\u5fd7\u4e2d\u7684\u54cd\u5e94\u6587\u672c\u4e2d\u83b7\u5f97\u5b8c\u6574\u7684 html\uff0c\u5e76\u4e14 <code>#formerrormessage<\/code> \u5b9e\u9645\u4e0a\u663e\u793a html\uff08\u5df2\u89e3\u6790\uff09 \uff09\u5305\u542b\u6240\u6709\u56fe\u50cf\u548c\u6587\u672c\u3002\u5f53\u6211\u53ea\u662f\u5237\u65b0\u9875\u9762\u65f6\uff0c\u5b83\u4f1a\u8f6c\u5230\u767b\u5f55\u9875\u9762\uff0c\u7136\u540e\u6211\u5fc5\u987b\u4ece\u90a3\u91cc\u6ce8\u9500\u3002<\/p>\n<pre>w.Header().Set(\"Location\", \"\/\")<\/pre>\n<p>\u6211\u60f3\u77e5\u9053\u8fd9\u662f\u6b63\u5e38\u884c\u4e3a\u8fd8\u662f\u6211\u9057\u6f0f\u4e86\u4e00\u4e9b\u4e1c\u897f\u3002\u6211\u6700\u521d\u4f7f\u7528\u666e\u901a\u7684 <code>http.redirect<\/code> \u6765\u5b9e\u73b0\u8fd9\u4e2a\uff0c\u6ca1\u6709 ajax\uff0c\u5b83\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u73b0\u5728\u6211\u8bd5\u56fe\u4f7f\u7528 ajax \u8ba9\u5b83\u5de5\u4f5c\uff0c\u4f46\u5b83\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u60a8\u5728\u670d\u52a1\u5668\u4e0a\u8bbe\u7f6e location \u6807\u5934\u65f6\uff0c\u5b83\u4ec5\u9488\u5bf9\u8fd9\u4e00 ajax \u8bf7\u6c42\u8fdb\u884c\u8bbe\u7f6e\u3002\u5b83\u88ab\u6b63\u786e\u5730\u91cd\u5b9a\u5411\u5230 <code>\/<\/code>\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u60a8\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u8be5 html\u3002<\/p>\n<p>\u7531\u4e8e\u6b64\u8bf7\u6c42\u4e0e\u60a8\u7684\u201c\u6b63\u5e38\u201d\u7f51\u7edc\u6d4f\u89c8\u5668\u6d41\u91cf\u5206\u5f00\uff0c\u56e0\u6b64\u60a8\u5fc5\u987b\u81ea\u5df1\u5904\u7406 javascript \u4ee3\u7801\u4e2d\u7684\u91cd\u5b9a\u5411\uff0c\u4f8b\u5982\u5c06\u5176\u653e\u5165\u6210\u529f\u51fd\u6570\u4e2d\uff1a<\/p>\n<pre>document.location = document.location.protocol + \"\/\/\" + document.location.host<\/pre>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u7528\u4e8e\u767b\u5f55\u7684 api \u7aef\u70b9\uff0c\u8be5\u7aef\u70b9\u8fd4\u56de\u767b\u5f55\u662f\u5426\u6210\u529f\uff0c\u7136\u540e\u5728 ajax \u8bf7\u6c42\u4e2d\u8c03\u7528\u6b64\u7aef\u70b9\u3002\u6210\u529f\u540e\uff0c\u60a8\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u5fc5\u987b\u5904\u7406\u91cd\u5b9a\u5411\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aAjax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Ajax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411 Ajax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411 \u6765\u6e90\uff1astackoverflow 2024-04-30 12:30:35 0\u6d4f\u89c8 \u6536\u85cf \u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aAjax \u91cd\u5b9a\u5411\uff0chttp.StatusOK \u7531 ajax \u9519\u8bef\u5904\u7406\u4e14\u672a\u91cd\u5b9a\u5411\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002 \u95ee\u9898\u5185\u5bb9 \u6211\u5728 go \u4e2d\u6709 func\uff08\u6211\u5df2\u7ecf\u5220\u9664\u4e86\u6240\u6709\u9519\u8bef\u5904\u7406\u4ee5\u4f7f\u5176\u7b80\u77ed\uff09 func postloginajax(w http.responsewriter, r *http.request) { if r.method == &#8220;post&#8221; { useremail := r.formvalue(&#8220;email&#8221;) userpassword := r.formvalue(&#8220;password&#8221;) db := r.context().value( &#8220;db&#8221;).(*sqlx.db) sessionstore := r.context().value( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43173","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43173","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=43173"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43173\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}