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

时间:2022-10-08 18:27:23

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脚本有问题,这是一个基本的简化版本,即使它不能工作,我有jquery脚本调用的php文件,我将它设置为编码并显示json响应

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

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

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

是json。响应json字符串中调用名为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>

UPDATE;

更新;

I have changed
json.response

我改变了json.response

into

data.response

data.response

But this did not make it ork either

但这也没有使它变成ork

4 个解决方案

#1


28  

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>

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.

这里的主要问题是您返回的JSON中有一个错码(“resonse”而不是“response”)。这意味着您在JavaScript代码中寻找了错误的属性。未来发现这些问题的一种方法是安慰。记录数据的值,并确保正在查找的属性在那里。

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

学习如何使用Chrome调试器工具(或类似的工具在Firefox/Safari/Opera/etc.)也将是无价的。

#2


5  

You should use data.response in your JS instead of json.response.

您应该使用数据。用你的JS而不是json.response。

#3


4  

Your PHP array is defined as:

您的PHP数组定义为:

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

Notice the mispelling "resonse". Also, as RaYell has mentioned, you have to use data instead of json in your success function because its parameter is currently data.

注意到mispelling“resonse”。同样,正如RaYell提到的,您必须在success函数中使用数据而不是json,因为它的参数是当前的数据。

Try editing your PHP file to change the spelling form resonse to response. It should work then.

尝试编辑PHP文件,将拼写表单改成response。它应该工作。

#4


0  

Look out for this pitfal: http://www.vertstudios.com/blog/avoiding-ajax-newline-pitfall/

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

Searched several houres before I found there were some linebreaks in the included files.

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

#1


28  

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>

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.

这里的主要问题是您返回的JSON中有一个错码(“resonse”而不是“response”)。这意味着您在JavaScript代码中寻找了错误的属性。未来发现这些问题的一种方法是安慰。记录数据的值,并确保正在查找的属性在那里。

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

学习如何使用Chrome调试器工具(或类似的工具在Firefox/Safari/Opera/etc.)也将是无价的。

#2


5  

You should use data.response in your JS instead of json.response.

您应该使用数据。用你的JS而不是json.response。

#3


4  

Your PHP array is defined as:

您的PHP数组定义为:

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

Notice the mispelling "resonse". Also, as RaYell has mentioned, you have to use data instead of json in your success function because its parameter is currently data.

注意到mispelling“resonse”。同样,正如RaYell提到的,您必须在success函数中使用数据而不是json,因为它的参数是当前的数据。

Try editing your PHP file to change the spelling form resonse to response. It should work then.

尝试编辑PHP文件,将拼写表单改成response。它应该工作。

#4


0  

Look out for this pitfal: http://www.vertstudios.com/blog/avoiding-ajax-newline-pitfall/

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

Searched several houres before I found there were some linebreaks in the included files.

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