邮政方法返回404,即使获取方法返回302
问题
我有这个假API:
[{"task_id":"44","task_title":"task1"},{"task_id":"45","task_title":"task2"},{"task_id":"46","task_title":"task1"},{"task_id":"47","task_title":"task2"}]
当我尝试使用以下代码使用Fetch Get方法访问它时:
function getData(){
fetch('http://localhost:5000/api/tasks')
.then(res => res.json())
.then((data) => {console.log(data)});}
getData();
i 获得预期的结果。
(4) [{…}, {…}, {…}, {…}]
0: {task_id: '44', task_title: 'task1'}
1: {task_id: '45', task_title: 'task2'}
2: {task_id: '46', task_title: 'task1'}
3: {task_id: '47', task_title: 'task2'}
但是,当我尝试通过帖子方法访问它时,例如:
function postData(){
fetch('http://localhost:5000/api/tasks',{
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
task_title: 'testTask'
})
})
.then(res =>{
return res.json()
})
.then(data => console.log(data))
.catch(error => console.log('ERROR'))
}
我得到了这一出乎意料的404:
POST http://localhost:5000/api/tasks 404 (Not Found)
使用node.js和Express:
app.get('/api/tasks', (req, res)=>{
db.SelectAll().then(data => {res.json(data)});
})
db只是:
const db = require('./db/db.js');
和selectall()是:
async function SelectAll() {
try {
const res = await client.query("SELECT * FROM tasks");
return res.rows;
} catch(err){
console.log(err);
}
client.end();
};
是什么引起了这一404 ?
The problem
I have this fake API:
[{"task_id":"44","task_title":"task1"},{"task_id":"45","task_title":"task2"},{"task_id":"46","task_title":"task1"},{"task_id":"47","task_title":"task2"}]
When I try to access it using a fetch GET method, using the code below:
function getData(){
fetch('http://localhost:5000/api/tasks')
.then(res => res.json())
.then((data) => {console.log(data)});}
getData();
I get the expected result in the console.
(4) [{…}, {…}, {…}, {…}]
0: {task_id: '44', task_title: 'task1'}
1: {task_id: '45', task_title: 'task2'}
2: {task_id: '46', task_title: 'task1'}
3: {task_id: '47', task_title: 'task2'}
Yet, when I try to access it through a POST method, like so:
function postData(){
fetch('http://localhost:5000/api/tasks',{
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
task_title: 'testTask'
})
})
.then(res =>{
return res.json()
})
.then(data => console.log(data))
.catch(error => console.log('ERROR'))
}
I get this unexpected 404:
POST http://localhost:5000/api/tasks 404 (Not Found)
My API is set as so using Node.js and express:
app.get('/api/tasks', (req, res)=>{
db.SelectAll().then(data => {res.json(data)});
})
db is just:
const db = require('./db/db.js');
and SelectAll() is:
async function SelectAll() {
try {
const res = await client.query("SELECT * FROM tasks");
return res.rows;
} catch(err){
console.log(err);
}
client.end();
};
What is causing this 404?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码登记一个Get Handler。您尚未使用
app.post
注册邮政处理程序。虽然此应该意味着该路径的邮政请求不允许响应响应,但 express不支持此,而是给出
404,找不到
。如果您想在向该路径提出发布请求时做某事,则需要使用
app.post
注册将运行并执行您想做的任何事情的函数。Your code registers a GET handler. You have not used
app.post
to register a POST handler.While this should mean that a POST request to that path gets a
405 Method Not Allowed
response, Express does not support this automatically and gives a404 Not Found
instead.If you want to do something when a POST request is made to that path, then you need to use
app.post
to register a function that will run and do whatever it is that you want to do.