【问题标题】:CORS error on axios POST request to backendaxios POST 请求到后端时出现 CORS 错误
【发布时间】:2020-04-03 09:39:18
【问题描述】:

我使用 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'
        }

      })




【问题讨论】:

    标签: javascript http cors axios


    【解决方案1】:
    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
            })
    });
    
    
    

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 2021-06-18
      • 2022-01-23
      • 2020-10-26
      • 2022-11-17
      • 1970-01-01
      • 2022-12-11
      • 2021-06-07
      • 2019-09-29
      相关资源
      最近更新 更多