如何从jquery脚本中将jSON响应转换为变量

我在下面的jquery脚本遇到问题,这是一个基本的剥离版本,即使它不起作用,我有jquery脚本调用的php文件,我将它设置为编码并显示json响应

然后在jquery脚本中它应该读取值并响应它但它没有得到响应。

json.response是错误的方法来调用名称响应的json字符串中的变量吗?

有人可以帮助我,我被卡住了

'error','comment'=>'test comment here'); echo json_encode($arr); ?> //the script above returns this: {"response":"error","comment":"test comment here"}  $.ajax({ type: "POST", url: "process.php", data: dataString, dataType: "json", success: function (data) { if (json.response == 'captcha') { alert('captcha'); } else if (json.response == 'error') { alert('sorry there was an error'); } else if (json.response == 'success') { alert('sucess'); }; } })  

更新;

我改变了
json.response

data.response

但这并没有使它成为ork

这是脚本,重写为使用上面的建议和更改你的无缓存方法。

 'error','comment'=>'test comment here'); echo json_encode($arr); ?> //the script above returns this: {"response":"error","comment":"test comment here"}  

这里的主要问题是你输入的JSON中有一个拼写错误(“共鸣”而不是“响应”。这意味着你在JavaScript代码中寻找错误的属性。将来解决这些问题的一种方法是console.log data的值,并确保您正在寻找的属性。

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

您应该在JS中使用data.response而不是json.response

您的PHP数组定义为:

 $arr = array ('resonse'=>'error','comment'=>'test comment here'); 

注意拼写错误的“共鸣”。 另外,正如RaYell所提到的,你必须在你的success函数中使用data而不是json ,因为它的参数当前是data

尝试编辑PHP文件以更改拼写forms以response 。 它应该工作。

留意这个pitfal: http : //www.vertstudios.com/blog/avoiding-ajax-newline-pitfall/

在我发现包含的文件中有一些换行符之前搜索了几个小时。