WordPress \u524d\u7aef\u6295\u7a3f<\/a> \u8868\u5355\uff0c\u7528\u6237\u70b9\u51fb \u201c\u7acb\u5373\u6295\u7a3f\u201d \u540e\uff0c\u4f1a\u901a\u8fc7 Rest API \u53d1\u5e03\u4e00\u4e2a\u72b6\u6001\u4e3a\u8349\u7a3f\u7684\u6587\u7ae0\u5230 WordPress \u540e\u53f0\u3002\u901a\u8fc7 Rest API \u5230 WordPress \u540e\u53f0\u9700\u8981\u9a8c\u8bc1\u624d\u80fd\u63d0\u4ea4\uff0c\u56e0\u4e3a\u662f\u5728\u540c\u4e00\u4e2a\u7ad9\u70b9\u63d0\u4ea4\u6570\u636e\uff0c\u6211\u4eec\u4f7f\u7528\u6700\u57fa\u672c\u7684 \u201cNonce\u201d \u9a8c\u8bc1\u65b9\u6cd5\u5373\u53ef\u3002\u8fd9\u79cd\u65b9\u6cd5\u9996\u5148\u9700\u8981\u8bbe\u7f6e nonce \u968f\u673a\u6570\u5230\u524d\u7aef\uff0c\u4ee5\u4fbf Axios \u5e93\u4f7f\u7528\u3002\u5148\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801\u5230 WordPress \u7684 functions.php \u6587\u4ef6\u4e2d\u3002<\/p>\n\n\n\nadd_action( 'wp_enqueue_scripts', 'rest_theme_scripts' );\nfunction rest_theme_scripts() {\n wp_localize_script( 'jquery', 'wp', [\n 'nonce' => wp_create_nonce( 'wp_rest' ),\n ] );\n}<\/code><\/pre>\n\n\n\n \u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8bbe\u7f6e\u4e00\u4e2a\u540d\u79f0\u4e3a \u201cwp\u201d \u7684 JavaScript \u5bf9\u8c61\u5230\u9875\u9762\u7684 head \u4e2d\uff0c\u7136\u540e\u5728\u4e0b\u9762\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7 wp.nonce<\/code> \u8bbf\u95ee WordPress \u540e\u53f0\u751f\u6210\u7684\u8fd9\u4e2a\u968f\u673a\u6570\u4e86\uff0c\u628a\u8fd9\u4e2a\u968f\u673a\u6570\u52a0\u5165\u5230 Axios \u7684 http header \u4e2d\uff0cRest API \u4f1a\u5bf9\u8fd9\u4e2a\u968f\u673a\u6570\u8fdb\u884c\u9a8c\u8bc1\uff0c\u5982\u679c\u9a8c\u8bc1\u4e00\u81f4\uff0c\u5c31\u53ef\u4ee5\u4fdd\u5b58\u63d0\u4ea4\u7684\u6570\u636e\u4e86\uff0c\u5982\u679c\u9a8c\u8bc1\u4e0d\u901a\u8fc7\uff0c\u5219\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002 <\/p>\n\n\n\n<template>\n <el-form ref=\"form\" :model=\"form\" label-width=\"80px\">\n\n <el-c-alert v-if=\"message.show\" :title=\"message.title\" :type=\"message.type\"><\/el-c-alert>\n\n <el-form-item label=\"\u6587\u7ae0\u6807\u9898\">\n <el-input v-model=\"form.title\"><\/el-input>\n <\/el-form-item>\n <el-form-item label=\"\u6587\u7ae0\u6458\u8981\">\n <el-input type=\"textarea\" v-model=\"form.excerpt\"><\/el-input>\n <\/el-form-item>\n <el-form-item label=\"\u6587\u7ae0\u5185\u5bb9\">\n <el-input type=\"textarea\" v-model=\"form.content\"><\/el-input>\n <\/el-form-item>\n <el-form-item>\n <el-button type=\"primary\" @click=\"onSubmit\">\u7acb\u5373\u6295\u7a3f<\/el-button>\n <el-button>\u53d6\u6d88<\/el-button>\n <\/el-form-item>\n <\/el-form>\n<\/template>\n\n<script>\n import axios from 'axios';\n\n \/\/ \u8bbe\u7f6e axios \u5168\u5c40 header nonce \u6570\u636e\uff0c\u7528\u4e8e WordPress REST Api \u9a8c\u8bc1\uff0c\n \/\/ \u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\uff0c\u63d0\u4ea4\u6570\u636e\u65f6\uff0c\u4f1a\u56e0\u4e3a\u6743\u9650\u9a8c\u8bc1\u5931\u8d25\u800c\u63d0\u4ea4\u4e0d\u4e86\n axios.defaults.headers.post['X-WP-Nonce'] = wp.nonce;\n\n export default {\n data() {\n return {\n form : {\n title : '',\n excerpt : '',\n content : '',\n },\n message: {\n title: \"\",\n type : \"\",\n show : false\n }\n };\n },\n\n methods: {\n onSubmit() {\n axios.post('http:\/\/abc.dev\/wp-json\/wp\/v2\/posts', {\n title : this.form.title,\n excerpt : this.form.excerpt,\n content : this.form.content,\n })\n .then(response => {\n this.message.title = \"\u4fdd\u5b58\u6210\u529f\";\n this.message.type = \"success\";\n this.message.show = true;\n })\n .catch(e => {\n this.errors.push(e)\n });\n }\n }\n\n };\n<\/script><\/code><\/pre>\n\n\n\n\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u7528\u6237\u63d0\u4ea4\u6210\u529f\u540e\uff0c\u4f1a\u663e\u793a\u4e00\u4e2a\u63d0\u4ea4\u6210\u529f\u7684\u63d0\u793a\u6d88\u606f\u3002\u4e3a\u4e86\u7cbe\u7b80\u6587\u7ae0\u7684\u7bc7\u5e45\uff0c\u6ca1\u6709\u628a\u63d0\u4ea4\u5931\u8d25\u7684\u5904\u7406\u65b9\u6cd5\u8d34\u51fa\u6765\uff0c\u6240\u4ee5\u5982\u679c\u63d0\u4ea4\u5931\u8d25\uff0c\u4e0d\u4f1a\u6709\u4efb\u4f55\u63d0\u793a\u3002\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u4e00\u4e0b\u3002<\/p>\n\n\n\n
\u521b\u5efa\u901a\u7528\u7684 http \u6a21\u5757\uff0c\u65b9\u4fbf\u5728\u5404\u4e2a\u6a21\u5757\u4e2d\u8c03\u7528<\/h2>\n\n\n\n
\u4e3a\u4e86\u65b9\u4fbf\u5728\u591a\u4e2a\u6a21\u5757\u76f4\u63a5\u8c03\u7528\u6570\u636e\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u901a\u7528\u7684 http \u6a21\u5757\uff0c\u5728\u5176\u4ed6\u6a21\u5757\u4f7f\u7528\u65f6\uff0c\u76f4\u63a5\u5f15\u5165\u8fd9\u4e2a\u6a21\u5757\u5373\u53ef\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n
<script>\n import axios from 'axios';\n\n export const HTTP = axios.create({\n baseURL: `http:\/\/jsonplaceholder.typicode.com\/`,\n headers: {\n Authorization: 'Bearer {token}'\n }\n })\n<\/script><\/code><\/pre>\n\n\n\n \u5728\u5176\u4ed6\u6a21\u5757\u4f7f\u7528\u65f6\uff1a <\/p>\n\n\n\n
<script>\n import {HTTP} from '.\/http-common';\n\n export default {\n data: () => ({\n posts: [],\n errors: []\n }),\n\n created() {\n HTTP.get(`posts`)\n .then(response => {\n this.posts = response.data\n })\n .catch(e => {\n this.errors.push(e)\n })\n }\n }\n<\/script><\/code><\/pre>\n\n\n\n \u719f\u6089\u4e0a\u4e0a\u9762\u7684\u5f00\u53d1\u65b9\u6cd5\u4ee5\u540e\uff0c\u5728 WordPress \u524d\u7aef\u9875\u9762\u83b7\u53d6\u63d0\u4ea4\u6570\u636e\u90fd\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 WordPress Rest API \u8fdb\u884c\uff0c\u53ea\u8981\u719f\u7ec3 JavaScript\uff0c\u5b8c\u5168\u53ef\u4ee5\u7ed3\u5408 WordPress \u505a\u4e00\u4e2a\u529f\u80fd\u4e30\u5bcc\uff0c\u754c\u9762\u6f02\u4eae\u7684\u5e94\u7528\u4e86\uff0c\u6bd4\u5982 WordPress \u4f7f\u7528\u8005\u7ecf\u5e38\u5bfb\u627e\u7684\u524d\u7aef\u7528\u6237\u4e2d\u5fc3\uff0c\u751a\u81f3\u57fa\u4e8eWordPress \u7684\u8d2d\u7269\u8f66\u8ba2\u5355\u7cfb\u7edf\u7b49\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u672c\u6587\u4ecb\u7ecd\u7684\u65b9\u6cd5\u8fdb\u884c\u5f00\u53d1\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"
\u57fa\u4e8e Vue.js \u7684 Element UI \u5b9e\u73b0\u524d\u7aef\u754c\u9762\u6837\u5f0f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1044],"tags":[185,718,204],"collection":[],"class_list":["post-11922","post","type-post","status-publish","format-standard","hentry","category-uncategory","tag-wp-development","tag-wordpresszk"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/posts\/11922"}],"collection":[{"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/comments?post=11922"}],"version-history":[{"count":1,"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/posts\/11922\/revisions"}],"predecessor-version":[{"id":277272,"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/posts\/11922\/revisions\/277272"}],"wp:attachment":[{"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/media?parent=11922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/categories?post=11922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/tags?post=11922"},{"taxonomy":"collection","embeddable":true,"href":"https:\/\/www.npc.ink\/wp-json\/wp\/v2\/collection?post=11922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}