客户端生成的 JSON 存储在 MongoDB 服务器端,application/json 没有正确解析?
Client-generated JSON to be stored in MongoDB serverside, application/json not parsing correctly?
我会很简短;我有一个 client-side form that allows for creation of "quizzes", a series of questions with answers attached to them, to be sent off to the server to be stored to a MongoDB. I've tried to do this in in accordance with W3's specs,但承诺的结果与我得到的结果不匹配。
我希望我的 JSON 由嵌套对象组成,如下所示:
{
"_id": "54a884c68999af900fc28dcb",
"name": "testquiz",
"author": "user",
"questions": [
{
"text": "question one",
"answers": [
{
"text": "answer one",
"correct": false
},
{
"text": "answer two",
"correct": true
}
]
},
{
"text": "question two",
"answers": [
{
"text": "answer one",
"correct": true
},
{
"text": "answer two",
"correct": false
}
]
}
]
}
但是,我只能得到这样的结果:
{
"_id": "54a8b00108039068102f8835",
"quizname": "World War II",
"questions[0][text]": "When did WWII start?",
"questions[0][answers][0][text]": "1938",
"questions[0][answers][1][iscorrect]": "on",
"questions[0][answers][1][text]": "1939",
"questions[0][answers][2][text]": "1944",
"questions[0][answers][3][text]": "1914",
"questions[1][text]": "",
"questions[1][answers][0][text]": "",
"questions[1][answers][1][text]": "",
"author": "user"
}
我做错了什么?我是 MEAN 堆栈的新手,感谢任何帮助。
我明白了。我花了很尴尬的时间和大量的搜索,但它有效。
$('#savequiz').click(function () {
obj = {
quizname: $('#quizname').val(),
author: "",
questions: []
};
for (var i = 0; i < questionCount; i++) {
obj.questions.push({
text: $('#question_' + i).val(),
answers: []
});
for (var j = 0; j < answerCount[i]; j++) {
obj.questions[i].answers.push({
text: $('#question_' + i + '_answer_' + j + ' > div > input').val(),
correct: $('#question_' + i + '_answer_' + j + ' > div > span > input[type="checkbox"]').is(':checked')
});
}
}
$.ajax({
url: '/quiz/new',
type: "POST",
data: JSON.stringify(obj),
processData: false,
contentType: "application/json; charset=UTF-8"
});
return false;
});
我相信解决方案体现在 $.ajax 调用参数中 'processData: false'
这仍然没有回答我的好奇心,为什么 W3 的 application/json
表单提交规范不符合规范,但至少我让它起作用了,并且我发布了这个,希望它能帮助其他人遇到同样的问题。好久没遇到这么简单的事情,结果却很费时间。
如果它有任何相关性或兴趣,这就是我处理请求的方式:
app.post('/quiz/new', function (req, res, next) {
if (!req.session.loggedin) {
res.status(401).end();
return;
}
var quiz = req.body;
quiz.author = req.session.username;
db.collection('quiz', function (err, collection) {
collection.insert(quiz, {safe: true}, function (err, result) {
if (err) {
console.log('Failed to persist quiz!');
res.send({'error': 'An error has occurred'});
return;
}
console.log("Successfully persisted quiz.");
res.redirect('/');
});
});
我会很简短;我有一个 client-side form that allows for creation of "quizzes", a series of questions with answers attached to them, to be sent off to the server to be stored to a MongoDB. I've tried to do this in in accordance with W3's specs,但承诺的结果与我得到的结果不匹配。
我希望我的 JSON 由嵌套对象组成,如下所示:
{
"_id": "54a884c68999af900fc28dcb",
"name": "testquiz",
"author": "user",
"questions": [
{
"text": "question one",
"answers": [
{
"text": "answer one",
"correct": false
},
{
"text": "answer two",
"correct": true
}
]
},
{
"text": "question two",
"answers": [
{
"text": "answer one",
"correct": true
},
{
"text": "answer two",
"correct": false
}
]
}
]
}
但是,我只能得到这样的结果:
{
"_id": "54a8b00108039068102f8835",
"quizname": "World War II",
"questions[0][text]": "When did WWII start?",
"questions[0][answers][0][text]": "1938",
"questions[0][answers][1][iscorrect]": "on",
"questions[0][answers][1][text]": "1939",
"questions[0][answers][2][text]": "1944",
"questions[0][answers][3][text]": "1914",
"questions[1][text]": "",
"questions[1][answers][0][text]": "",
"questions[1][answers][1][text]": "",
"author": "user"
}
我做错了什么?我是 MEAN 堆栈的新手,感谢任何帮助。
我明白了。我花了很尴尬的时间和大量的搜索,但它有效。
$('#savequiz').click(function () {
obj = {
quizname: $('#quizname').val(),
author: "",
questions: []
};
for (var i = 0; i < questionCount; i++) {
obj.questions.push({
text: $('#question_' + i).val(),
answers: []
});
for (var j = 0; j < answerCount[i]; j++) {
obj.questions[i].answers.push({
text: $('#question_' + i + '_answer_' + j + ' > div > input').val(),
correct: $('#question_' + i + '_answer_' + j + ' > div > span > input[type="checkbox"]').is(':checked')
});
}
}
$.ajax({
url: '/quiz/new',
type: "POST",
data: JSON.stringify(obj),
processData: false,
contentType: "application/json; charset=UTF-8"
});
return false;
});
我相信解决方案体现在 $.ajax 调用参数中 'processData: false'
这仍然没有回答我的好奇心,为什么 W3 的 application/json
表单提交规范不符合规范,但至少我让它起作用了,并且我发布了这个,希望它能帮助其他人遇到同样的问题。好久没遇到这么简单的事情,结果却很费时间。
如果它有任何相关性或兴趣,这就是我处理请求的方式:
app.post('/quiz/new', function (req, res, next) {
if (!req.session.loggedin) {
res.status(401).end();
return;
}
var quiz = req.body;
quiz.author = req.session.username;
db.collection('quiz', function (err, collection) {
collection.insert(quiz, {safe: true}, function (err, result) {
if (err) {
console.log('Failed to persist quiz!');
res.send({'error': 'An error has occurred'});
return;
}
console.log("Successfully persisted quiz.");
res.redirect('/');
});
});