添加自定义标头Vue时请求失败

时间:2020-10-22 15:14:37

标签: vue.js request fetch

我正在使用Axios进行请求,但是添加自定义标头(API需要自定义标头)时,此操作将失败。 如果添加自定义标头,则响应为:

# A tibble: 4 x 2 filingdate mycolumn <date> <dbl> 1 2020-09-29 -83171612 2 2020-10-01 -5186952 3 2020-10-02 -91921 4 2020-10-16 -12286605

我已经看到有两个请求: 1-冷杉要求: first request

2-秒请求 second request

在API中启用了CORS

has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

我在Vue中的请求

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers:  Origin, X-Requested-With, Content- 
Type, Accept, Access-Control-Request-Method,t-id-establecimiento,bt- 
username,bt-nombre-empleado,ipaddress");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Allow: GET, POST, OPTIONS, PUT, DELETE");

我的自定义标题:

let headers = this.getHeader()
    this.$http(`${mUrl}`,{headers})
      .then(function (resp) {
         console.log(resp.data)

      })
 .catch(function (err) {
  console.log('rerrrr' + err)
})

1 个答案:

答案 0 :(得分:-2)

在处理CORS错误时,大多数情况下,您不需要修改请求标头。 99%的时间,问题出在后端。确保在以下位置允许CORS:

  • 服务器配置和
  • 应用程序

如果使用XAMPP或任何Apache服务器,则默认情况下未启用CORS。尽管您已经允许在应用程序上使用CORS,但服务器仍将阻止它。

请注意:更改配置文件时,请确保重新启动服务器

相关问题