作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 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/
我是一名优秀的程序员,十分优秀!