[英]Getting json array in Ajax
我正在尝试从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"}
]
}
用于获取数据的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
它看起来不像data.testlog需要解析,你可以这样做
var testlog = data.testlog
for(var i = 0; i < testlog.length; i++){
var temp = 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>')
}
另外,我还更改了代码中的几项内容(间距,使用单引号而不是双引号以及对对象的调用值使用点表示法。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.