![](/img/trans.png)
[英]GET http://localhost:5000/user 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.