我試圖用httpRequest動態地從php請求填充javascript數組beaches3。將http字符串傳入Javascript數組使用httpRequest
var jqxhr = $.get("http://127.0.0.1/websites/map_with_me.php?region="+region+"&suburb="+suburb+"&lat="+lat+"&lon="+lng, function() {
document.getElementById("thediv").innerHTML = (jqxhr.responseText);
var beaches3 = (jqxhr.responseText);
})
map_with_me.php的輸出
[
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.423036, 151.259052, 5],
['Cronulla Beach', -34.028249, 121.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.450198, 151.259302, 1]
];
所以我想,以填補由map_with_me.php文件生成的動態位置beaches3變種。
如果我將整個$ .get請求替換爲靜態泳灘變量,它將起作用。
如何將由php文件生成的動態javascript傳遞給javascript數組?
map_with_me.php
<?php
echo "var beaches3 = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.423036, 151.259052, 5],
['Cronulla Beach', -34.028249, 121.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.450198, 151.259302, 1]
];"
?>
這工作:
var jqxhr = $.get("http://127.0.0.1/websites/map_with_me.php?region="+region+"&suburb="+suburb+"&lat="+lat+"&lon="+lng, function() {
document.getElementById("thediv").innerHTML = (jqxhr.responseText);
var beaches3 = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.423036, 151.259052, 5],
['Cronulla Beach', -34.028249, 121.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.450198, 151.259302, 1]
];
})
這並不:
var jqxhr = $.get("http://127.0.0.1/websites/map_with_me.php?region="+region+"&suburb="+suburb+"&lat="+lat+"&lon="+lng, function() {
document.getElementById("thediv").innerHTML = (jqxhr.responseText);
var beaches3 = (jqxhr.responseText);
})
謝謝,我返回一個PHP字符串「[ [ '邦迪海灘',-33.890542,151.274856,4], [ 'Coogee海灘',-33.423036,151.259052 ,5], [ '努拉海灘',-34.028249,121.157507,3], [ 'Manly海灘',-33.80010128657071,151.28747820854187,2], [ '馬魯巴海灘',-33.450198,151.259302,1] ] ;」 – preschool 2012-04-29 01:54:10
是的,這是數組內部的數組,並且爲了將beaches3變量變成具有相同結構和值的數組,上面的代碼應該工作得很好。 – adeneo 2012-04-29 02:04:18
感謝@adeneo,我明白你的意思:php文件是第一個數組內的數組。我怎樣才能解決這個問題? – preschool 2012-04-29 02:17:16