0

I am trying to insert selections from a second data source into an Autocomplete dropdown. Both 1st and 2d source fetches are via ajax. I crafted and hard coded a JSON array that works to insert 2d source data into Autocomplete's dropdown. It passes the jsonLint test. The format is the following:

[{"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", }]

I'm having major trouble creating code to put variable data into that type and format. The database currently contains JSON strings. I use PHP's json_encode() to create the data object passed to the browser. The database entries could be changed, but they now look like:

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

It seems that 1. the JSON strings don't get changed to objects, and 2. some extraneous, unknown characters get inserted somehow. However, I'm a novice, especially with functions and objects. Here is the jQuery/Javascript, plus some comments about the state of the data at various points.

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

Note that the isArray test at bD1 is true and the isArray test at bD2 is false, and the only thing that happens between them is closing off the function and the ajax. Also note the comment line with the hash # mark that documents the seemingly extra " " marks.

How do I get the boroughData function to output a JSON array with the format that I showed in the first code box above? Please make your advice specific and with code. I'm a novice and don't follow general instructions well.

4

1 回答 1

0

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");
于 2013-01-02T22:35:49.280 回答