簡體   English   中英

通過JQuery.Ajax將字符串轉換為JSON

[英]Convert the String to JSON, getting by JQuery.Ajax

我的PHP代碼通過此鏈接getPower.php?year=2012&country=tr產生了類似的結果

"Kasan":[[1,50]]
"Tasan":[[1,51],[2,52],[3,52]]
"Hasan":[[1,50]]
"Masan":[[1,51],[2,52],[3,52]]

有了這個代碼;

$row = $result->fetch_assoc();

echo json_encode($row['teamName']). ":" ;

putPowerbyTeam($db,$row['teamID'],$year); //End with echo "json_encode($returnArray);"  

echo "\n";

我用這個代碼將它們轉換為JavaScript對象;

$.ajax({type: "GET",
url: "getPower.php",
data: {year : "year", country : "country"},
success: function(JSONText) {

    var lines = JSONText.split('\n');

    $.each(lines,function(lineNo,line)
                    {
        var mainItems = line.split(':');
        chart.series[lineNo].name = jQuery.parseJSON(eval(mainItems[0]));
        chart.series[lineNo].setData(jQuery.parseJSON(eval(mainItems[1])), true);
    });

},
error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
    alert(thrownError);
}

我錯了,說不能拆分null。 因此,返回null,但是我不確定此javascrip代碼的任何部分。

那么,為什么返回null? 我能做什么? 其余的好嗎?

您可以像這樣使用dataType

$.ajax({type: "GET",
url: "getPower.php",
data: {year : "year", country : "country"},
dataType: 'json',
success: function(JSONText) {

// do something

不要嘗試使用多個echojson_encode構建JSON。 使用一個大的關聯數組,然后echo json_encode(array)

像這樣的東西:

$array = array();

$teamName = $row['teamName'];

$array[$teamName] = putPowerbyTeam($db,$row['teamID'],$year); // Return the array instead of doing json_encode($returnArray);

echo json_encode(array);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM