Javascript 使用节点js发布请求
我正在尝试使用node.js发出post请求,当我尝试运行它时,我会将数据显示在控制台中,但不会显示在HTML的主体中。在控制台中,我得到了错误信息Javascript 使用节点js发布请求,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试使用node.js发出post请求,当我尝试运行它时,我会将数据显示在控制台中,但不会显示在HTML的主体中。在控制台中,我得到了错误信息 app.js:4 POST http://localhost:8000/addAnimal net::ERR_EMPTY_RESPONSE postData @ app.js:4 (anonymous) @ app.js:25 app.js:21 Uncaught (in promise) TypeError: Failed to fetch 看
app.js:4 POST http://localhost:8000/addAnimal net::ERR_EMPTY_RESPONSE
postData @ app.js:4
(anonymous) @ app.js:25
app.js:21 Uncaught (in promise) TypeError: Failed to fetch
看起来该函数正在工作,但实际的post请求部分没有工作。我一辈子都搞不清楚我做错了什么
这是我的代码:
server.js:
projectData = {};
/* Express to run server and routes */
const express = require('express');
/* Start up an instance of app */
const app = express();
/* Dependencies */
const bodyParser = require('body-parser')
/* Middleware*/
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const cors = require('cors');
app.use(cors());
/* Initialize the main project folder*/
app.use(express.static('project1'));
const port = 8000;
/* Spin up the server*/
const server = app.listen(port, listening);
function listening(){
// console.log(server);
console.log(`running on localhost: ${port}`);
};
// GET route
app.get('/all', sendData);
function sendData (request, response) {
response.send(projectData);
};
// POST route
app.post('/add', callBack);
function callBack(req,res){
res.send('POST received');
}
// POST an animal
const data = [];
// TODO-Call Function
app.route('/addAnimal')
.get(function (req, res) {
res.sendFile('index.html', {root: 'project1'})
})
.post(function (req, res) {
data.push(req.body)
})
app.js
/* Function to POST data */
const postData = async ( url = '', data = {})=>{
console.log(data);
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data), // body data type must match "Content-Type" header
});
try {
const newData = await response.json()
// console.log(newData);
return newData.json()
}catch(error) {
console.log("error", error)
// appropriately handle the error
};
};
// TODO-Call Function
postData('/addAnimal', {animal:'lion'});
非常感谢您的帮助
谢谢,
迈克
使用以下命令更改app.js代码
/* Function to POST data */
const postData = async (url = "", data = {}) => {
const response = await fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
try {
return response.json();
} catch (error) {
console.log("error", error);
// appropriately handle the error
}
};
// TODO-Call Function
(async function(){
let res = await postData("/addAnimal", { animal: "lion" });
console.log(res);
})();
也可以像下面这样更改post方法
app.route('/addAnimal')
.get(function (req, res) {
res.sendFile('index.html', {root: 'project1'})
})
.post(function (req, res) {
data.push(req.body);
res.status(200).send(data);
})
使用以下命令更改app.js代码
/* Function to POST data */
const postData = async (url = "", data = {}) => {
const response = await fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
try {
return response.json();
} catch (error) {
console.log("error", error);
// appropriately handle the error
}
};
// TODO-Call Function
(async function(){
let res = await postData("/addAnimal", { animal: "lion" });
console.log(res);
})();
也可以像下面这样更改post方法
app.route('/addAnimal')
.get(function (req, res) {
res.sendFile('index.html', {root: 'project1'})
})
.post(function (req, res) {
data.push(req.body);
res.status(200).send(data);
})
试试这个:
app.route('/addAnimal')
.get(function (req, res) {
res.sendFile('index.html', {root: 'project1'})
})
.post(function (req, res) {
data.push(req.body);
res.send('done'); // send response
});
试试这个:
app.route('/addAnimal')
.get(function (req, res) {
res.sendFile('index.html', {root: 'project1'})
})
.post(function (req, res) {
data.push(req.body);
res.send('done'); // send response
});
我也这么想,但不确定,因为OP也看到了
app.js:21 Uncaught(in promise)TypeError:Failed to fetch
我仍然没有看到HTML正文中的结果。知道为什么吗?发布数据时,您是否检查了浏览器中的console.log??有什么?我也这么想,但不确定,因为OP也看到了app.js:21 Uncaught(in promise)TypeError:Failed to fetch
我仍然没有看到HTML正文中的结果。知道为什么吗?发布数据时,您是否检查了浏览器中的console.log??有什么?你可能想检查你可能想检查我仍然没有在HTML的主体中看到结果。知道为什么吗?我仍然没有在HTML的主体中看到结果。知道为什么吗?