gpt4 book ai didi

javascript - 如何在 Javascript 中访问这些数据?

转载 作者:行者123 更新时间:2023-12-01 01:43:30 26 4
gpt4 key购买 nike

我将 JSON 格式的编码作为返回的数据:

{
"error": {
"msg":"Concurrent verifications to the same number are not allowed",
"code":10
}
}

我想访问 msg,所以我将 javascript 编写为:

$("#buttonPhoneSubmit").click(function(e) {
$("#verifyForm").show();
e.preventDefault();
$.ajax({
type: 'post',
url: './process.php',
data: $('form').serialize(),
datatype:'json',
success: function (data) {
$('#error_message').html(data.error.msg);
}
});

但它说数据未定义。有人可以告诉我代码有什么问题吗?

谢谢!

最佳答案

Roy said ,您有 datatype: 'json' 而不是 dataType: 'json'。所以我怀疑 jQuery 没有为你解析 JSON。

虽然您可以将其更改为 dataType: 'json',但更好的方法是更新 PHP 文件以发送 Content-Type: 应用程序/json header 和响应:

// In the PHP, prior to sending the body of the response
header('Content-Type: application/json');

...并从 ajax 调用中删除 datatype: 'json'。 jQuery 将查看内容类型并为您解析它,此时您的代码应该可以工作(假设页面返回返回您引用的 JSON)。

关于javascript - 如何在 Javascript 中访问这些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52160388/

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