【问题标题】:problems with type and format out of a jQuery .map() functionjQuery .map() 函数的类型和格式问题
【发布时间】:2012-12-07 12:00:49
【问题描述】:

我正在尝试将来自第二个数据源的选择插入到自动完成下拉列表中。第 1 次和第 2 次源代码获取都是通过 ajax 进行的。我制作并硬编码了一个 JSON 数组,用于将 2d 源数据插入到 Autocomplete 的下拉列表中。它通过了 jsonLint 测试。格式如下:

[{"label":"New York, Bronx, Bronx County, New York, United States","value":"Bronx, Bronx County, New York, United States"},
{"label":"New York, Brooklyn, Kings County, New York, United States","value":"Brooklyn, Kings County, New York, United States", }]

我在创建将可变数据放入该类型和格式的代码时遇到了很大的麻烦。数据库当前包含 JSON 字符串。我使用 PHP 的 json_encode() 来创建传递给浏览器的数据对象。数据库条目可以更改,但它们现在看起来像:

"label":"New York, Bronx, Bronx County, New York, United States","value":"Bronx, Bronx County, New York, United States"

似乎 1. JSON 字符串没有更改为对象,以及 2. 一些无关的未知字符以某种方式插入。但是,我是新手,尤其是函数和对象。这是 jQuery/Javascript,以及一些关于不同点数据状态的 cmets。

var boroughData = function() {
    $.ajax({
        //normal stuff like url:, dataType:, etc. It works.
        success: function (data){
            boroughData = $.map( data, function (item){
                //data is JSON, but info inside extra " ", like Object { boroughString=""label":"Dallas, Cockre..., Texas, United States""}
                // # response like [{"boroughString":"\"label\":\"Dallas, Cockrell Hill, Dallas County, Texas, United States\", . . ."}]
                return item.boroughString;//returns JSON strings, not objects, in an array
            });
            alert(jQuery.isArray(boroughData)  + "|bD1"); //true
            console.log(boroughData); //array containing JSON strings in red
        } //closes success: for boroughData
    }); //closes ajax for boroughData
}(); //closes boroughData and (); executes it
alert(jQuery.isArray(boroughData)  + "|bD2"); //false
alert(boroughData + "|bD3"); //"label":"Dallas, Cockrell Hill, Texas", "value":"Dallas, Cockrell Hill, Texas","label":"Dallas, Downtown Dallas, Texas", "value":"Dallas, etc.
alert(JSON.parse(boroughData) + "|bD4");//SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

请注意,bD1 处的 isArray 测试为真,bD2 处的 isArray 测试为假,它们之间唯一发生的事情就是关闭函数和 ajax。另请注意带有井号 # 标记的注释行,其中记录了看似额外的“ ”标记。

如何让 boroughData 函数以上面第一个代码框中显示的格式输出 JSON 数组?请让您的建议具体并带有代码。我是新手,不能很好地遵循一般说明。

【问题讨论】:

  • 嗨。我不能理解你的意思。特别是我看不到如何将其应用于自动完成插件。我会根据你的代码给你答案。

标签: javascript json jquery types jquery-ui-autocomplete


【解决方案1】:

IMO 你的函数没有返回值。 我不确定你的方式是否真的能正确执行功能,但相信如此:)

    function boroughData () {
    $.ajax({
        success: function (data){
            boroughData = $.map( data, function (item){
                return item.boroughString;//returns JSON strings, not objects, in an array
            });
            alert(jQuery.isArray(boroughData)  + "|bD1"); //true
            console.log(boroughData); 
        } 
    });
    return boroughData; // it's not good practice to use the same name for local variable... 
    }(); 
    alert(jQuery.isArray(boroughData)  + "|bD2"); //false
    alert(boroughData + "|bD3");
    alert(JSON.parse(boroughData) + "|bD4");

【讨论】:

  • Saram,感谢您的评论,即在具有该变量名称的函数中对局部变量使用相同的名称是不好的做法。我会改变它。您的代码未以我描述的所需格式返回数据,但我感谢您的努力。我还在解决这个问题。
猜你喜欢
  • 2018-12-18
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
相关资源
最近更新 更多