栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从jquery脚本获取jSON响应到变量中

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从jquery脚本获取jSON响应到变量中

这是脚本,使用上面的建议进行了重写,并对无缓存方法进行了更改。

<?php// Simpler way of making sure all no-cache headers get sent// and understood by all browsers, including IE.session_cache_limiter('nocache');header('Expires: ' . gmdate('r', 0));header('Content-type: application/json');// set to return response=error$arr = array ('response'=>'error','comment'=>'test comment here');echo json_enpre($arr);?>//the script above returns this:{"response":"error","comment":"test comment here"}<script type="text/javascript">$.ajax({    type: "POST",    url: "process.php",    data: dataString,    dataType: "json",    success: function (data) {        if (data.response == 'captcha') { alert('captcha');        } else if (data.response == 'success') { alert('success');        } else { alert('sorry there was an error');        }    }}); // Semi-colons after all declarations, IE is picky on these things.</script>

这里的主要问题是您在返回的JSON中有一个错字(“ resonse”而不是“
response”。这意味着您在Javascript代码中查找了错误的属性。一种在将来捕获这些问题的方法)是

console.log
的价值
data
,并确保你正在寻找的属性是存在的。

学习如何使用Chrome调试器工具(或Firefox / Safari / Opera /等中的类似工具)也将非常宝贵。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427403.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号