gpt4 book ai didi

javascript - 如何通过ajax获取json字符串并转换为php数组?

转载 作者:行者123 更新时间:2023-11-28 18:46:17 25 4
gpt4 key购买 nike

我是 ajax 和 json 新手。

我这里有两个文件,名称为 script.js 和 SubmitComquality.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"}]

在submitCompliance.php中,有一段代码:

<?php include_once("../connection.php"); ?>
<?php
$objQuestion=json_decode($_REQUEST['objQuestion']);
$question=array();
$question=$objQuestion;
print_r($question);?>

但是当我运行submitCompressive.php时,它给出了有关未定义索引的错误:objQuestion

请帮帮我。

最佳答案

改变
数据:“objQuestion =”+str_objQuestion,

 data:{"objQuestion:"+str_objQuestion},

关于javascript - 如何通过ajax获取json字符串并转换为php数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com