我是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
请帮我在这里。
最佳答案
更改data:"objQuestion="+str_objQuestion,
至
data:{"objQuestion:"+str_objQuestion},