Node.js 从Axios/React向Node/Express服务器发布json时出现问题

Node.js 从Axios/React向Node/Express服务器发布json时出现问题,node.js,reactjs,post,axios,Node.js,Reactjs,Post,Axios,我目前正在使用Node.js开发一个web应用程序,后端使用Express,前端使用React.js。在尝试通过axios将用户数据发布到节点服务器时,我遇到了一个问题。当我使用x-www-form-urlencoded内容类型进行发布时,前端将发布到服务器,但发布数据的整个JSON显示在第一个元素的键字段中。当我将内容类型更改为json时,它将停止从前端发布任何内容。我已经尝试了向服务器进行卷曲,卷曲一篇JSON文章将被服务器接受 对发送到服务器的代码作出反应 handleSubmit()

我目前正在使用Node.js开发一个web应用程序,后端使用Express,前端使用React.js。在尝试通过axios将用户数据发布到节点服务器时,我遇到了一个问题。当我使用x-www-form-urlencoded内容类型进行发布时,前端将发布到服务器,但发布数据的整个JSON显示在第一个元素的键字段中。当我将内容类型更改为json时,它将停止从前端发布任何内容。我已经尝试了向服务器进行卷曲,卷曲一篇JSON文章将被服务器接受

对发送到服务器的代码作出反应

handleSubmit()
  {
    var form=this;
    var axiosConfig = {
      headers: {
        'content-type': 'application/json; charset=utf-8'
      }
    }

    axios.post('http://localhost:8080/api/login/', {
      'username': form.state.username,
      'password': form.state.password
    }, {headers: {'content-type': 'application/json'}});

  };
api端点的服务器代码

//From server.js
const express=require('express');
const session=require('express-session');
const bodyParser=require("body-parser");
const path = require('path');

var login = require('./routers/login')

var port = process.env.PORT || 8080;
var app=express();

app.use(session({'secret': 'thealphabetbackwardsiszyxwvutsrqponmlkjihgfedcba'}));
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());

//...

app.use('/api/login', login);

//from login.js
/* Router for login system
   When user attempts to log in, check their credentials
   If the user successfully logs in, create a session
   If user enters invalid credentials prompt them
*/

const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const user = require("./../models/UserSchema")
mongoose.connect('mongodb://localhost/newt');

const express = require('express');
const router = express.Router();

router.get('/', function (req, res)
{
  console.log("Test");
})

router.post('/', function(req, res)
{
  console.log(req.body);
  res.end();
})

// To create test user use path localhost:8080/api/login/testing
router.get('/test', function (req, res)
{
  var db = mongoose.connection;

  var test = new user({
    username: "joesephschomseph",
    email: "testUser@test.com",
    fname: "Joe",
    lname: "Schmoe"
})

test.save();

console.log("Created test user!");
});


module.exports = router

  • npm安装--保存常量主体解析器
  • 在app.js中include const bodyparser=require('body-parser')
  • 删除“用户名”和“密码”中的单引号

  • 控制台日志(请求主体)
    试试没有配置的axios.post(URL,DATA),它应该是默认的post-jsonI已经尝试删除配置了,我根本就找不到任何可以发布的东西。谢谢。我已经安装并包含了body解析器,我只是忘了在我的帖子中包含该代码。删除单引号似乎没有任何区别,因为成功地将react应用程序添加到postOk我现在看到了你的问题。您正在向require('./routers/login')下的此登录函数发送/api/login;因此,这就是您想要编写console.log(req.body)的地方,您还需要确保它有一个post端点。你能发布login.js文件中的代码吗?只需编辑它以包含login.js的完整代码。我不确定它是否与端点有任何关系,因为我可以使用
    curl-xpost-H“Content-Type:application/json”-d'{“username”:“test”}发布到它http://localhost:8080/api/login/
    如果使用curl发布,console.log是否正常工作?req.body是否已填充?是的,如果我使用curl发布,则body将填充正确格式的
    {username:“test”}
    ,如果我使用
    内容类型:application/x-www-url-encoded
    从客户端发布,它将发布,但console.log调用将打印{“'username':'test'…”:“},我发送给服务器的JSON的完整性被放入第一个值的键中。
    app.use(bodyparser.urlencoded({ extended: false }));
    app.use(bodyparser.json());