错误 WordPress REST API V2 调用 Vue.js

Error WordPress REST API V2 call with Vue.js

当我使用 Vue.js 和 axios 调用 WP REST API 时,出现以下错误。

XMLHttpRequest cannot load http://my-wordpress-site.com/wp-json/wp/v2/posts. The 'Access-Control-Allow-Origin' header has a value 'http://null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.

当我使用带有 GET 方法的 Postman 时,它工作正常。

问题出在哪里?

var app = new Vue({
  el: '#app',
  data: {
    posts: [],
  },
  mounted: function() {
    this.getPosts()
  },
  methods: {
    getPosts: function() {
      var app = this

      axios.get('http://my-wordpress-site.com/wp-json/wp/v2/posts')
      .then(function (response) {
        app.posts = response.data.title.rendered
      })
      .catch(function (error) {
        console.log(error)
      })
    }
  }
});
<div id="app">
  <div class="section">
    <ul>
      <li v-for="post in posts">{{ post }}</li>
    </ul>
    <h3></h3>
  </div>
</div>

我通过更改 wp-includes/rest-api.php 文件中的一行解决了它,
在 rest_send_cors_headers() 函数中:

header( 'Access-Control-Allow-Origin: *');