我是ajax和json的新手。
我在这里有两个文件名为script.js和submitComprehension.php。
在script.js中,我有一个名为submit()的函数。这里,objQuestion已经有值并且成功地转换为json字符串。 代码:
function submit(){
var str_objQuestion=JSON.stringify(objQuestion);
$.ajax({
data:"objQuestion="+str_objQuestion,
url:"submitComprehension.php",
success:function(msg){
alert(msg);
}
})
}
这是转换为字符串后的json值。
[{"qid":820,"question":"Comprehension-1/Question-4","opt_a":"Comprehension-1/Question-4/Option-A","opt_b":"Comprehension-1/Question-4/Option-B","opt_c":"Comprehension-1/Question-4/Option-C","opt_d":"Comprehension-1/Question-4/Option-D","user_ans":"a","cor_ans":"D"},{"qid":822,"question":"Comprehension-1/Question-6","opt_a":"Comprehension-1/Question-6/Option-A","opt_b":"Comprehension-1/Question-6/Option-B","opt_c":"Comprehension-1/Question-6/Option-C","opt_d":"Comprehension-1/Question-6/Option-D","user_ans":"b","cor_ans":"C"},{"qid":819,"question":"Comprehension-1/Question-3","opt_a":"Comprehension-1/Question-3/Option-A","opt_b":"Comprehension-1/Question-3/Option-B","opt_c":"Comprehension-1/Question-3/Option-C","opt_d":"Comprehension-1/Question-3/Option-D","user_ans":"c","cor_ans":"C"},{"qid":823,"question":"Comprehension-1/Question-7","opt_a":"Comprehension-1/Question-7/Option-A","opt_b":"Comprehension-1/Question-7/Option-B","opt_c":"Comprehension-1/Question-7/Option-C","opt_d":"Comprehension-1/Question-7/Option-D","user_ans":"d","cor_ans":"D"},{"qid":826,"question":"Comprehension-1/Question-10","opt_a":"Comprehension-1/Question-10/Option-A","opt_b":"Comprehension-1/Question-10/Option-B","opt_c":"Comprehension-1/Question-10/Option-C","opt_d":"Comprehension-1/Question-10/Option-D","user_ans":"a","cor_ans":"C"},{"qid":821,"question":"Comprehension-1/Question-5","opt_a":"Comprehension-1/Question-5/Option-A","opt_b":"Comprehension-1/Question-5/Option-B","opt_c":"Comprehension-1/Question-5/Option-C","opt_d":"Comprehension-1/Question-5/Option-D","user_ans":"d","cor_ans":"A"},{"qid":824,"question":"Comprehension-1/Question-8","opt_a":"Comprehension-1/Question-8/Option-A","opt_b":"Comprehension-1/Question-8/Option-B","opt_c":"Comprehension-1/Question-8/Option-C","opt_d":"Comprehension-1/Question-8/Option-D","user_ans":"d","cor_ans":"A"},{"qid":825,"question":"Comprehension-1/Question-9","opt_a":"Comprehension-1/Question-9/Option-A","opt_b":"Comprehension-1/Question-9/Option-B","opt_c":"Comprehension-1/Question-9/Option-C","opt_d":"Comprehension-1/Question-9/Option-D","user_ans":"c","cor_ans":"B"},{"qid":817,"question":"Comprehension-1/Question-1","opt_a":"Comprehension-1/Question-1/Option-A","opt_b":"Comprehension-1/Question-1/Option-B","opt_c":"Comprehension-1/Question-1/Option-C","opt_d":"Comprehension-1/Question-1/Option-D","user_ans":"b","cor_ans":"A"},{"qid":818,"question":"Comprehension-1/Question-2","opt_a":"Comprehension-1/Question-2/Option-A","opt_b":"Comprehension-1/Question-2/Option-B","opt_c":"Comprehension-1/Question-2/Option-C","opt_d":"Comprehension-1/Question-2/Option-D","user_ans":"a","cor_ans":"B"}]
在submitComprehension.php中,这是一个代码:
<?php include_once("../connection.php"); ?>
<?php
$objQuestion=json_decode($_REQUEST['objQuestion']);
$question=array();
$question=$objQuestion;
print_r($question);?>
但是当我运行submitComprehension.php时,它会给出关于未定义索引的错误:objQuestion
请帮帮我。
答案 0 :(得分:3)
更改
Account
到
data:"objQuestion="+str_objQuestion,
答案 1 :(得分:0)
更新您的提交功能,如下所示:
function submit(){
var str_objQuestion=JSON.stringify(objQuestion);
$.ajax({
data:{objQuestion : str_objQuestion},
url:"submitComprehension.php",
success:function(msg){
alert(msg);
}
})