我正在使用 HTML/CSS/JS 和 AJAX 連接到服務器的站點上作業。(網路開發新手)
我目前有這個來獲取資料并將其放入陣列中。
var addresses = [];
$.ajax({
type: "GET",
url: 'GetAddresses.php',
data: {"type":"check"},
success: function(response){
alert(response);
addresses.push(response) // add values from php to array
}
});
但是如果 php 回顯一個地址、一個名字和一個城市,例如。我怎樣才能訪問這些不同的值?
謝謝您的幫助。
uj5u.com熱心網友回復:
通常,您會撰寫輸出結構化資料(例如 JSON)的 PHP,然后在客戶端上對其進行決議。
<?php
header("Content-Type: application/json");
$data = [ "address" => "foo", "name" => "bar", "city" => "baz" ];
echo json_encode($data);
?>
然后在客戶端:
$.ajax({
type: "GET",
url: 'GetAddresses.php',
data: {"type":"check"},
success: function(response){
alert(response);
console.log(response.address);
console.log(response.name);
console.log(response.city);
}
});
與您的問題的焦點無關,從 Ajax 回呼將資料推送到更廣泛范圍內的陣列可能會給您帶來問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/424069.html
標籤:javascript php html 阿贾克斯
