为什么我的 JSON 值没有解析为对象?
这是我的jquery部分
$.ajax({
url:'url.php',
type:'post',
data:{'players':playersDone, 'userID':userID, 'gameVariant':gameVariant, 'gameWeek':gameWeek,},
success: function (res) {
console.log(res);
alert(res);
alert(res.idOfLastRow);
window.location.href = " summary.php?idOfLastEntry='"+res.idOfLastRow+"' ";
}
});
我的php看起来像这样:
...
...
'".$_POST['players']['fw3']."',
".$_POST['players']['sub']."',
'',
'');";
mysqli_query($connection, $queryInsertFinishedDraft);
$_POST['players']['idOfLastRow'] = $connection->insert_id;
JSON 数据
当我从警报中的响应中输出收到的数据时,它看起来像这样
{"df1":"952","idOfLastRow":77}
但是,当我尝试访问其中一个字段时,我得到未定义:
alert(res.idOfLastRow)
为“未定义”
最佳答案
在 ajax 调用中,您需要添加数据类型 json
url:'url.php',
type:'post',
dataType: 'json', <---
data:{'players':playersDone, 'userID':userID, 'gameVariant':gameVariant, 'gameWeek':gameWeek,},
success: function (res) {
关于javascript - jquery:json请求输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918817/