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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:02:06  来源:igfitidea点击:

How to get jSON response into variable from a jquery script

jqueryajaxjson

提问by JasonDavis

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.response 是在名称响应的 json 字符串中调用变量的错误方法吗?

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

数据响应

But this did not make it ork either

但这也没有让它成为

回答by Nathan Kleyn

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.logthe value of dataand make sure the property you are looking for is there.

这里的主要问题是您返回的 JSON 中有一个拼写错误(“resonse”而不是“response”。这意味着您在 JavaScript 代码中寻找错误的属性。未来解决这些问题的一种方法是console.log价值,data并确保您正在寻找的财产在那里。

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

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

回答by RaYell

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

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

回答by John Rasch

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 datainstead of jsonin your successfunction because its parameter is currently data.

请注意拼写错误的“ resonse”。此外,正如 RaYell 所提到的,您必须在函数中使用data而不是,因为它的参数当前是.jsonsuccessdata

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

尝试编辑您的 PHP 文件以将拼写形式更改resonseresponse. 它应该工作。

回答by Sven

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

注意这个陷阱:http: //www.vertstudios.com/blog/avoiding-ajax-newline-pitfall/

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

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