// responseData is fetched via Ajax-Get
var obj = jQuery.parseJSON(responseData);
alert(obj.property);
// full ajax example
jQuery.ajax({
url: "/index.php?action=whereMyDataIs", // here php gives the json response
type: "GET",
dataType: "json",
async: false,
success: function (data) {
console.log(data);
alert(JSON.stringify(data));
}
});
您已經有PHP的一部分(陣列構建,json_encoded和發送的響應),接下來是你取的客戶端這json_response,通過做一個AJAX GET請求到PHP腳本提供響應。通過指定dataType:「json」,您可以告訴jQuery自動將傳入數據解析爲Object Notation。如果你想再輸出一次,例如有了警報,你需要再次將其串聯起來。
解答有關amcharts問題:
// add the loadJson function to AmCharts
// but you could also use jQuery as demonstrated above
AmCharts.loadJSON = function(url) {
// create the request
if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari
var request = new XMLHttpRequest();
} else {
// code for IE6, IE5
var request = new ActiveXObject('Microsoft.XMLHTTP');
}
// load it
// the last "false" parameter ensures that our code will wait before the
// data is loaded
request.open('GET', url, false);
request.send();
// parse adn return the output
return eval(request.responseText);
};
// init chart library
var chart;
// lets build that chart
AmCharts.ready(function() {
// load the data from your php script (the json response)
var chartData = AmCharts.loadJSON('script.php');
// verify in browser console, if the data is loaded and parsed correctly
console.log(chartData);
// build the chart
// ...
// output the chart
chart.write("chartdiv");
});
你跟阿賈克斯獲得此爲有頭和一切? – adeneo
是的,這是我的ajax的迴應。這些變量被加起來創建ajax中的總數。現在我想把這3個總數放在我放入的數組中,並將它們轉換爲js數組對象。 – mattnewbie