如何获得JSON响应转变为可变从一个jQuery脚本 [英] How to get jSON response into variable from a jquery script

查看:88
本文介绍了如何获得JSON响应转变为可变从一个jQuery脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我下面的jQuery脚本的麻烦,这是一个基本的精简版,甚至是行不通的,我有PHP文件的jQuery脚本将调用到,我把它设置为en code和显示JSON响应

I am having trouble with my jquery script below, this is a basic stripped down version and even it will not work, I have the php file that the jquery script makes a call to, I have it set to encode and show a json response

然后在jQuery脚本它应该阅读的价值,并对此作出回应,但没有得到回应。

Then in the jquery script it should read the value and respond to it but It is not getting the response.

时json.response错误的方式来调用JSON字符串,名称响应变量?

Is json.response the wrong way to call a variable in the json string that is name response?

有人可以帮忙,请我被困

Can someone help please I am stuck

<?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>

更新;

我已经改变
json.response

I have changed
json.response

data.response

data.response

但这并没有使它扫要么

推荐答案

下面的脚本,重写使用上述建议和改变你的无缓存的方法。

Here's the script, rewritten to use the suggestions above and a change to your no-cache method.

<?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>

这里

主要的问题是,你必须在JSON一个错字,你正在返回(resonse而不是反应。这意味着,你找错了属性在JavaScript中code。的一种方式在未来捕捉这些问题的方法是的console.log 数据值,并确保你正在寻找物业是存在的。

The main issue here was that you had a typo in the JSON you were returning ("resonse" instead of "response". This meant that you were looking for the wrong property in the JavaScript code. One way of catching these problems in the future is to console.log the value of data and make sure the property you are looking for is there.

学习如何使用Chrome调试工具(或类似工具在Firefox / Safari浏览器/歌剧/等)也将是非常宝贵的。

Learning how to use the Chrome debugger tools (or similar tools in Firefox/Safari/Opera/etc.) will also be invaluable.

这篇关于如何获得JSON响应转变为可变从一个jQuery脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆