首页 > 解决方案 > Vue. js CORS issues, backend data blocked by CORS policy

问题描述

I am using Vuejs with CLI 3.6.3 and i am facing CORS issue so i can not get the backend api data.

The error message is :

Access to XMLHttpRequest at 'http://...' 
from origin 'http://localhost:8080' 
has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

I do create a vue.config.js file :

module.exports = {
    devServer: {
      proxy: 'https://my/site/myapi/myindex.php',
  }
}

import axios and declare crossDomain equals true:

import axios from "axios";
axios.defaults.crossDomain = true;

And at the backend i do declare:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');

How can i access the backend api data without facing CORS issues ?

标签: vue.jscors

解决方案


header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: *');

这应该是 api 接受请求的地方


推荐阅读