我需要一些幫助來設置DB標記檢索。我對於回傳到底是什麼感到困惑。以下是我迄今爲止:準備SQL - AJAX返回數據
返回的數據:
["Chatswood NSW AU","Chippendale NSW AU"]
JS:
var opdata = [];
function markers() {
$.post("id.php", {id: <?php echo $id; ?>})
.done(function(data) {
//data is array returned
opdata = data; //Tried this
$(opdata) = data; //Tried this
opdata.push(data); //Tried this
$.each(data, function(i) { //Tried this
opdata.push(data[i]);
});
});
console.log(opdata); //Shows [] empty array regardless what i do
}
PHP:
$arr = array();
while ( $selectData -> fetch()) {
$arr[] = $address;
}
echo json_encode($arr);
如何去檢索數據?以上都沒有工作。
這是推動我堅果..應該我只是$ .ajax而不是?
有什麼問題?你不能在'markers()'之外訪問'opdata'? – tymeJV
不能得到opdata裏面的函數,函數外面(數據) – user4713659
那麼數據調用是異步的,如果你想在調用之後使用它,請使用回調 – tymeJV