Node.js Reactjs axios post响应未返回任何内容

Node.js Reactjs axios post响应未返回任何内容,node.js,reactjs,axios,Node.js,Reactjs,Axios,我正在使用axios.post()编辑Reactjs应用程序后端的mysql数据库。数据通过post请求到达后端。但我需要知道post请求何时完成,并从中返回一些数据,以了解后端代码运行是否正常。我尝试了以下方法,其中newEdit是一个对象,其后端包含所需的信息 axios .post('http://ip:3001/edit_table', newEdit) .then((response) => { console.log("response: ",response)

我正在使用axios.post()编辑Reactjs应用程序后端的mysql数据库。数据通过post请求到达后端。但我需要知道post请求何时完成,并从中返回一些数据,以了解后端代码运行是否正常。我尝试了以下方法,其中newEdit是一个对象,其后端包含所需的信息

axios
 .post('http://ip:3001/edit_table', newEdit)
 .then((response) => { 
     console.log("response: ",response);
 }, (error) =>{
     console.log("error: ",error)
 });

两个控制台日志语句均未运行。再一次,对象确实到达了路由的nodejs文件,很好,我只是无法得到任何类型的响应。有人知道发生了什么吗?谢谢。

请确保您的后端正在向客户端返回响应。 您可以使用
res.send
res.json
。res.send([body])用于向客户端发送HTTP响应,而res.json(body)用于发送json响应

res.send([body])

res.json([body])

示例:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.send('hello world')
})

app.listen(3000)
var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.json({ success: true })
})

app.listen(3000)
参考文献:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.send('hello world')
})

app.listen(3000)
var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.json({ success: true })
})

app.listen(3000)
API参考


Node.js响应对象方法

确保后端正在向客户端返回响应。 您可以使用
res.send
res.json
。res.send([body])用于向客户端发送HTTP响应,而res.json(body)用于发送json响应

res.send([body])

res.json([body])

示例:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.send('hello world')
})

app.listen(3000)
var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.json({ success: true })
})

app.listen(3000)
参考文献:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.send('hello world')
})

app.listen(3000)
var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.json({ success: true })
})

app.listen(3000)
API参考


Node.js响应对象方法

如果您的后端代码正常并返回响应,那么您可以按照下面的示例进行操作,这非常有效

const updateData = async () => {
            try {
                const response = await axios.put(`https://jsonplaceholder.typicode.com/posts/${id}`, {
                    method: 'PUT',
                    body: JSON.stringify({
                        id: id,
                        title: post.title,
                        body: post.body,
                        userId: 1
                    }),
                    headers: {
                        "Content-type": "application/json; charset=UTF-8"
                    }
                })
                    .then(response => response.json())
                    .then(json => console.log(json));
                console.warn(response.data);
            } catch (error) {
                console.warn(error);
            }
        };

如果您的后端代码正常并返回响应,那么您可以按照下面的示例进行操作

const updateData = async () => {
            try {
                const response = await axios.put(`https://jsonplaceholder.typicode.com/posts/${id}`, {
                    method: 'PUT',
                    body: JSON.stringify({
                        id: id,
                        title: post.title,
                        body: post.body,
                        userId: 1
                    }),
                    headers: {
                        "Content-type": "application/json; charset=UTF-8"
                    }
                })
                    .then(response => response.json())
                    .then(json => console.log(json));
                console.warn(response.data);
            } catch (error) {
                console.warn(error);
            }
        };

这绝对是朝着正确的方向迈出的一步,我似乎找不到任何关于这个函数的文档。你知道我在哪里可以得到更多的信息吗?谢谢,我用例子和参考资料编辑了我的答案。我希望有帮助:)祝你好运!这绝对是朝着正确的方向迈出的一步,我似乎找不到任何关于这个函数的文档。你知道我在哪里可以得到更多的信息吗?谢谢,我用例子和参考资料编辑了我的答案。我希望有帮助:)祝你好运!