簡體   English   中英

POST http://localhost:9000/ net::ERR_CONNECTION_REFUSED

[英]POST http://localhost:9000/ net::ERR_CONNECTION_REFUSED

我正在嘗試通過發布請求向 api 發送一些數據。

前端代碼(反應)

handleSubmit = e => {
    e.preventDefault();
    fetch("http://localhost:9000", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        title: this.state.title,
        text: this.state.text
      })
    });
  };

服務器(快遞)

var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post("/", urlencodedParser, function(req, res) {
  sql = "INSERT INTO POSTS (id, title, text) VALUES ?";
  var values = [[uuidv1().toString(), req.body.title, req.body.text]];
  con.query(sql, [values], function(err, result, fields) {
    if (err) throw err;
  });
});

當我提交表單時,我從開發人員控制台收到此錯誤消息錯誤信息

我在localhost:3000上運行客戶端,在localhost:9000上運行服務器端。 有人可以讓我知道這個問題是如何發生的,我該如何解決?

我找到了解決方案。 顯然數據沒有被發布。 所以我必須用

headers: {'Content-Type':'application/x-www-form-urlencoded'}

在根目錄下的 package.json 文件中添加代理屬性,如:

{
    "proxy": "http://localhost:9000"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM