Axios发布请求由于CORS而失败,但是使用ajax的相同请求没有问题

问题描述 投票:0回答:1

我在执行axios发布请求时遇到以下错误。但是,当我使用ajax请求时,没有问题:

该请求已被CORS策略阻止:对预检请求的响应未通过访问控制检查:所请求的资源上没有'Access-Control-Allow-Origin'标头。

Ajax请求:

Axios请求:

let payload = {
    type: ["a", "b"],
    category: ["a", "b"],
    category: ["a", "b"],
    accountNumber: "123"

};
var apiURL = this.$apiBaseURL + "/Product/ProductDetails";
$.ajax({
    url: apiURL,
    type: "POST",
    data: { payload },
    xhrFields: {
        withCredentials: true
    },
    success: function (result) {
        console.log(JSON.stringify(result));
    }
});
this.$http.post(apiURL,payload,{withCredentials: true})
vue.js axios webapi
1个回答
1
投票
一种可行的方法是将axios中的Content-type标头设置为'Content-Type': 'application/x-www-form-urlencoded'

您必须在服务器端更改设置才能解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.