Reactjs req.body显示

我在ReactJs中有一个http put函数,看起来像这样

UpdateItem: function(_id) { var input = this.refs.myInput; $.ajax({ url: 'http://localhost:3000/update/' + _id, type: 'PUT', data:input, cache: false, success: function(result) { console.log(data); window.location.reload(); } }); } 

它需要一个input,它应该发送新的值与请求,但它永远不会

 <input ref="myInput" type="number" name="quantity" min="1" max="10" defaultValue={item.quantity} className="form-control"/> 

当我看着控制台req.body显示这个[对象对象],我在节点中的put函数看起来像这样

 app.put('/update/:_id', function(req, res) { console.log(req.params); console.log("your req is" +req.body); Cart.findByIdAndUpdate(req.params.id, { quantity: req.body.quantity }, function(err, product) { if (err) throw err; console.log(product); console.log(product); }); }); 

任何想法可能是什么问题?

发生这种情况是因为您正尝试将一个string与一个对象连接起来。 这就是你的console.log+ -sign所做的事情。

你会注意到console.log(req.body)console.log("your req is")都是单独工作的,但是console.log("your req is" + req.body)会给你错误的输出。

要解决这个问题,要么做两个单独的输出,要么使用这个:

 console.log("your req is", req.body); 

现在,您将在控制台中输出string和对象属性。

演示:

(检查控制台中的输出)

 var person = {name: "Bob", sirname: "Smith", age: 34}; console.log("The person data is: ", person); //right console.log("The person data is: " + person); //wrong 

它只是说你的“req.body”是某种对象,应该是。

如果你想看看里面是什么,这可能是方便的:

 console.log(JSON.stringify(req.body))