【问题标题】:axios post request retrning error: Cross-Origin Request Blockedaxios post请求返回错误:跨域请求被阻止
【发布时间】:2022-01-05 06:31:31
【问题描述】:

我正在使用 axios 将一些数据发布到我的节点服务器,但它给了我这个错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at MY_NODE_URL. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

这是我的代码:

 const url = "https://blooming-escarpment-74540.herokuapp.com/blog/postBlog";

 axios
    .post(url ,{
        title: document.getElementById("title").value,
        post: document.getElementById("post").value,
        blogImage:document.getElementById("blogImage").files[0],
     
    })
    .then((response) => {
      const result = response.data;
      if (result.status == "SUCCESS") {
       console.log("SUCCESS");
       
      } else {
        console.log("BAd");
      }
    })
    .catch((error) => {
      console.log(error);
    });

即使我使用fetch API 甚至XHR,这个错误仍然会显示

【问题讨论】:

    标签: javascript axios xmlhttprequest fetch-api


    【解决方案1】:

    你必须在你的 node js 后端安装 cors

    用法

    const express = require('express')
    const cors = require('cors');
    
    const app = express();
    app.use(cors());
    
    

    【讨论】:

    • 出现同样的错误
    猜你喜欢
    • 2017-02-06
    • 1970-01-01
    • 2018-06-28
    • 2015-06-08
    • 2019-10-11
    • 2019-02-22
    • 2015-05-05
    • 1970-01-01
    • 2019-11-04
    相关资源
    最近更新 更多