[英]JSON parse on a JSON string throws “Cannot convert object to primitive value”
server.js server.js
uses the body parser middleware, handlebars and express 使用主体解析器中间件,车把和快递
route 路线
module.exports = function (app) {
app.post('/questionnaire/submit', function (req, res) { // Ajax Call
var data = JSON.parse(req.body);
res.send({});
});
};
client 客户
function submitData() { // Send a data object to the server
$.ajax({
type: 'POST',
url: '/questionnaire/submit',
dataType: "json",
data: JSON.stringify({
satisfactory: "text 1",
improvement: "text 2",
rating: 0.7
})
}).done(function () {
$(location).attr('href', '/sendOff');
}).fail(function () {
});
}
when logging req.body
I get a JSON string 记录
req.body
我得到一个JSON字符串
{ '{"satisfactory":"text 1","improvement":"text 2","rating":0.7}': '' }
{'{“满意”:“文本1”,“改进”:“文本2”,“等级”:0.7}':“”}
and I try to parse this string to an object. 我尝试将此字符串解析为一个对象。 When I do so, I get this error message
这样做时,我会收到此错误消息
TypeError: Cannot convert object to primitive value at JSON.parse (<anonymous>) at C:\\Users\\mah\\Desktop\\FeedbackTool\\Server\\Routes\\questionnaire.js:12:25 at Layer.handle [as handle_request] (C:\\Users\\mah\\node_modules\\express\\lib\\router\\layer.js:95:5) at next (C:\\Users\\mah\\node_modules\\express\\lib\\router\\route.js:137:13) at Route.dispatch (C:\\Users\\mah\\node_modules\\express\\lib\\router\\route.js:112:3) at Layer.handle [as handle_request] (C:\\Users\\mah\\node_modules\\express\\lib\\router\\layer.js:95:5) at C:\\Users\\mah\\node_modules\\express\\lib\\router\\index.js:281:22 at Function.process_params (C:\\Users\\mah\\node_modules\\express\\lib\\router\\index.js:335:12) at next (C:\\Users\\mah\\node_modules\\express\\lib\\router\\index.js:275:10) at C:\\Users\\mah\\node_modules\\body-parser\\lib\\read.js:130:5
so how can I parse the string to an object? 那么如何将字符串解析为对象呢?
Normally I would execute JSON.parse(req.body)
. 通常我会执行
JSON.parse(req.body)
。
Since you are using body-parser
middleware then you dont have to parse req.body
again cause it already parsed by body-parser
由于您正在使用
body-parser
中间件,因此您req.body
再次解析req.body
,因为它已经由body-parser
进行了body-parser
Example 例
if you used app.use(bodyParser.json())
如果您使用了
app.use(bodyParser.json())
then you just have to do this 那么你只需要这样做
module.exports = function (app) {
app.post('/questionnaire/submit', function (req, res) { // Ajax Call
var data = req.body; // this is already parsed and is an object
res.send({});
});
};
as pointed out by @TJ Crowder you should also send the correct contentType
so body-parser
knows its json 正如@TJ Crowder指出的那样,您还应该发送正确的
contentType
以便body-parser
知道其json
function submitData() { // Send a data object to the server
$.ajax({
type: 'POST',
url: '/questionnaire/submit',
contentType: 'application/json',
data: JSON.stringify({
satisfactory: "text 1",
improvement: "text 2",
rating: 0.7
})
}).done(function () {
$(location).attr('href', '/sendOff');
}).fail(function () {
});
}
dataType
doesn't say what type of data you're sending , it says what type of response you expect. dataType
不会说您要发送什么类型的数据,它会说您期望什么类型的响应。 You need to say that you're sending JSON, via contentType: "application/json"
in your ajax
call. 您需要说您正在通过
ajax
调用中的contentType: "application/json"
发送JSON。 Details in the documentation . 文档中的详细信息。 (You're not the first or only person, by far, to be tripped up by the naming of
dataType
.) (到目前为止,您不是第一个或唯一一个被
dataType
命名的人。)
That's half the problem. 那是问题的一半。 See Stamos' answer for the other half.
另一半见Stamos的回答 。
You need to setup correct contentType: 您需要设置正确的contentType:
$.ajax({
type: 'POST',
url: '/questionnaire/submit',
contentType: 'application/json',
data: JSON.stringify({ satisfactory: "text 1", rating: 0.7 })
});
app.post('/questionnaire/submit', function (req, res) { // Ajax Call
var data = JSON.parse(req.body);
console.log(data.rating); // 0.7
res.send(data);
});
Also, with body-parser
you may avoid JSON.parse
calls on the server side, the link . 另外,使用
body-parser
可以避免在服务器端link上进行JSON.parse
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.