邮政方法返回404,即使获取方法返回302

发布于 2025-02-03 06:55:08 字数 1914 浏览 3 评论 0原文

问题

我有这个假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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2025-02-10 06:55:09
  app.get('/api/tasks',(req,res)=> {
  db.Selectall()。然后(data => {res.json(data)});
}))
 

您的代码登记一个Get Handler。您尚未使用app.post注册邮政处理程序。

虽然此应该意味着该路径的邮政请求不允许响应响应,但 express不支持此,而是给出404,找不到

如果您想在向该路径提出发布请求时做某事,则需要使用app.post注册将运行并执行您想做的任何事情的函数。

app.get('/api/tasks', (req, res)=>{
  db.SelectAll().then(data => {res.json(data)});
})

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 a 404 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文