axios POST请求到后端时出现CORS错误

tmb3ates  于 8个月前  发布在  iOS
关注(0)|答案(2)|浏览(87)

我使用AJAX发送登录请求到后端。但我试图做同样的事情与axios,但得到cors错误。这是我的请求。
CORS响应已在后端设置。

.post('https://testconsole.pva.com/j_spring_security_check', {
        data: {
          j_username: email,
          j_password: password
        },
        withCredentials: true,
        crossorigin: true,
        headers: {

          'Content-Type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json',
          'Access-Control-Request-Method': 'POST',
          'Access-Control-Request-Headers': 'X-PINGOTHER, Content-Type'
        }

      })

字符串

kknvjkwl

kknvjkwl1#

const api = axios.create({
  baseUrl: “https://testconsole.pva.com”,
  headers: {
    common: {  
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'WithCredentials': true,
          'Access-Control-Allow-Origin': '*'
});
....
const myRequest = await api({
  method: 'post',
  url: '/j_spring_security_check',
  data: JSON.stringify({
          j_username: email,
          j_password: password
        })
});

字符串

l3zydbqr

l3zydbqr2#

使用代理文件vue.js.js添加url cors错误url例如:您的url https://example.com/和错误url https://testconsole.pva.com/j_spring_security_check
将这些行添加到配置文件中

module.exports = {
    devServer: {
        proxy: 'https://testconsole.pva.com/'
    },
}

字符串
然后你可以尝试URL https://example.com/j_spring_security_check

相关问题