gpt4 book ai didi

javascript - Ajax中获取json数组

转载 作者:行者123 更新时间:2023-12-02 16:27:45 26 4
gpt4 key购买 nike

我正在尝试显示来自 ajax 响应的一些数据,如下所示。

$.post(
'/tests/elementary/'+($(this).attr('data-test-id'))+'/get-details', // location of your php script
{
}, // any data you want to send to the script
function( data ) { // a function to deal with the returned information
if(data.taken =='false'){
alert('ok')
}
else {

$('#test_name').empty().append(data.information);
$('#test_point').empty().append(data.details['score']);
$('#test_date').empty().append(data.updated_at);


for(var i=0;i<data.testlog.length;i++) {
var temp = data.testlog[i];
$("#test_details_body").append("<tr> <td>"+ (i+1) +"</td> <td>"+temp['operand1']+' '+temp['operator']+' '+temp['operand2']+"</td><td>"+temp['user_answer']+"<td>"+temp['correct'] +"</td><tr>")
}



}
});
});

但是我收到了 Uncaught SyntaxError: Unexpected token o 错误。

回复:

{
"success":"true",
"taken":"true",
"details":"{\"id\":2,\"user_id\":1,\"test_id\":9,\"token\":\"682c5de08481081b940364416cdce99d\",\"score\":80,\"attempts\":2,\"created_at\":\"2015-02-16 02:09:12\",\"updated_at\":\"2015-02-16 02:09:12\",\"course_id\":7}",
"information":"sample test exam",
"updated_at":"16-Feb-2015 02:02:12",
"testlog":[
{"id":21,"test_id":9,"user_id":1,"operand1":1,"operand2":10,"operator":"+","answer":11,"user_answer":11,"answering_time":"00:00:00","created_at":"2015-02-16 02:53:11","updated_at":"2015-02-16 02:53:11","token":"682c5de08481081b940364416cdce99d","correct":"1"},
{"id":22,"test_id":9,"user_id":1,"operand1":2,"operand2":10,"operator":"+","answer":12,"user_answer":12,"answering_time":"00:00:00","created_at":"2015-02-16 02:53:15","updated_at":"2015-02-16 02:53:15","token":"682c5de08481081b940364416cdce99d","correct":"1"},
{"id":23,"test_id":9,"user_id":1,"operand1":3,"operand2":10,"operator":"+","answer":13,"user_answer":0,"answering_time":"00:00:00","created_at":"2015-02-16 02:53:18","updated_at":"2015-02-16 02:53:18","token":"682c5de08481081b940364416cdce99d","correct":"1"},
{"id":24,"test_id":9,"user_id":1,"operand1":4,"operand2":10,"operator":"+","answer":14,"user_answer":0,"answering_time":"00:00:00","created_at":"2015-02-16 02:53:25","updated_at":"2015-02-16 02:53:25","token":"682c5de08481081b940364416cdce99d","correct":"0"},
{"id":25,"test_id":9,"user_id":1,"operand1":5,"operand2":10,"operator":"+","answer":15,"user_answer":0,"answering_time":"00:00:00","created_at":"2015-02-16 02:53:29","updated_at":"2015-02-16 02:53:29","token":"682c5de08481081b940364416cdce99d","correct":"1"}
]
}

enter image description here

用于获取数据的php脚本

public function getTestDetails($id){

$exists = Testresult::where('user_id', Auth::id())->where('test_id', $id)->first();

if($exists==null){
return Response::json(["success"=>"true", "taken"=>"false"]);
}
else{

$updated_at = date("d-M-Y H:m:s", strtotime($exists->updated_at));
$testLog = Testlog::where('token', $exists->token)->get();

$info = Test::where('id', $id)->pluck('name');
return Response::json(["success"=>"true", "taken"=>"true",
"details"=>"$exists",
"information"=>$info,
"updated_at"=>$updated_at,
"testlog"=>$testLog]);
}


}

我这样做对吗?

最佳答案

您走在正确的道路上!我认为这里有两个问题。第一个是您尝试将 JavaScript 对象解析为 JavaScript 对象。这是可见的,因为您的屏幕截图显示了通过 AJAX 调用正确解析大部分对象的响应。您在控制台日志中看到的错误证实了这一点。

因此删除下面的 JSON.parse(...) 将解决该问题:

for(var i=0;i<data.testlog.length;i++) {
var temp = data.testlog[i];
$("#test_details_body").append("<tr> <td>"+ (i+1) +"</td> <td>"+temp['operand1']+' '+temp['operator']+' '+temp['operand2']+"</td><td>"+temp['user_answer']+"<td>"+temp['correct'] +"</td><tr>")
}

第二个问题是 JSON 字符串的生成,正如您在评论中讨论的那样,它是通过 PHP 生成的。这对于回答原始问题不是必需的,但为了更好的实践,应该进行纠正。我还认为您可能认为必须解析数组,因为 JavaScript 对象中嵌入了 JSON 字符串。

我对 PHP 有点生疏,但我认为这一行 "details"=>"$exists", 实际上应该是 "details"=>$exists

关于javascript - Ajax中获取json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28552353/

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