我在下面的jquery脚本遇到问题,这是一个基本的精简版本,即使它不起作用,我有jquery脚本调用的php文件,我将它设置为编码并显示json响应
然后在jquery脚本中它应该读取值并响应它但它没有得到响应.
json.response是错误的方法来调用名称响应的json字符串中的变量吗?
有人可以请帮助我被困住了
<?PHP
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
// set to retunr response=error
$arr = array ('resonse'=>'error','comment'=>'test comment here');
echo json_encode($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 (json.response == 'captcha') {
alert('captcha');
} else if (json.response == 'error') {
alert('sorry there was an error');
} else if (json.response == 'success') {
alert('sucess');
};
}
})
</script>
Run Code Online (Sandbox Code Playgroud)
更新;
我改变了
json.response
成
data.response
但这并没有使它成为ork
Nat*_*eyn 28
这是脚本,重写为使用上面的建议和更改你的无缓存方法.
<?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_encode($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>
Run Code Online (Sandbox Code Playgroud)
这里的主要问题是你输入的JSON中有一个拼写错误("共鸣"而不是"响应".这意味着你在JavaScript代码中寻找错误的属性.将来解决这些问题的一种方法是console.log
值,data
并确保您正在寻找的属性存在.
学习如何使用Chrome调试工具(或Firefox/Safari/Opera等中的类似工具)也将非常宝贵.
归档时间: |
|
查看次数: |
112762 次 |
最近记录: |