简体   繁体   English

从jQuery AJAX调用解析JSON

[英]parse JSON from a jQuery AJAX call

I can successfully get back what looks like a JSON object, but I get a non-whitespace error when I parse it and when I don't parse it, I can't access the elements. 我可以成功找回看起来像JSON对象的对象,但是当我解析它并且不解析它时,我得到了一个非空白错误,我无法访问元素。 Basically, I just want to access each element in teh JSON and display it. 基本上,我只想访问JSON中的每个元素并显示它。 Here's the code: (below the code is my returned JSON (or what appears to be JSON) 这是代码:(下面的代码是我返回的JSON(或看起来是JSON的代码)

$('#cardText').change(function(){
            if($('#cardText').val().trim().length == 9)
                {
                    $.ajax({

                        url: 'components/Person.cfc',

                        //GET method is used
                        type: "POST",

                        //pass the data        
                        data: {
                            method: "getGroup",
                            uid: $('#cardText').val(),
                            },

                        success: function(response) {

                            //obj = jQuery.parseJSON(response); -- I get a non-whitespace error if I do this
                            var Col1 = response.COLUMNS[0]; -- this gives me response.Columns is undefined

                            $('#form_result').html(response);

                        },

                        error: function(jqXHR, exception) {
                            if (jqXHR.status === 0) {
                                alert('Not connect.\n Verify Network.');
                            } else if (jqXHR.status == 404) {
                                alert('Requested page not found. [404]');
                            } else if (jqXHR.status == 500) {
                                alert('Internal Server Error [500].');
                            } else if (exception === 'parsererror') {
                                alert('Requested JSON parse failed.');
                            } else if (exception === 'timeout') {
                                alert('Time out error.');
                            } else if (exception === 'abort') {
                                alert('Ajax request aborted.');
                            } else {
                                alert('Uncaught Error.\n' + jqXHR.responseText);
                            }
                        }
                }); 
            }
        });

returned data: 返回的数据:

{
    "COLUMNS": ["PLAN", "NAME", "ID", "ISSUE", "TYPE", "LASTUSED", "BALANCE"],
    "DATA": [["DINING STAFF CAFE 1919 ", "YOUNG, MARIA ", 8.03976343E8, "2001-04-02", 2.0, "2012-01-27", 1]]
}​

from notepad (limited the data) {"COLUMNS":["PLAN","NAME"],"DATA":[["DINING STAFF CAFE 1919 ","YOUNG, MARIA "]]} 来自记事本(限制了数据){“ COLUMNS”:[“ PLAN”,“ NAME”],“ DATA”:[[“ DINING STAFF CAFE 1919”,“ YOUNG,MARIA”]]}

It seems like you have a funky character after the last curly brace. 似乎在最后一个花括号之后您有一个时髦的角色。 Try pasting your JSON into Notepadd++. 尝试将JSON粘贴到Notepadd ++中。

There is a zero-width unicode character U+200b in the sample returned data you have pasted, between the closing } and the newline. 在您粘贴的示例返回数据中,在结束}和换行符之间有一个零宽度的Unicode字符U + 200b。 This prevented firefox from accepting the snippet. 这阻止了Firefox接受代码段。 How did it get there? 它是怎么到达那里的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM