我有這樣的問題。如何獲得沒有html頁面的Ajax響應?
<select onchange="select()" id="select">
<option value="1">test 1</option>
<option value="2">test 2</option>
</select>
我把#選擇的值與JavaScript和發送與AJAX到PHP
var clientVal = $('#select').val();
$.ajax({
type:'post',
url:'../../works/addWork.php',
dataType:'JSON',
data:{
client_val:clientVal
},
success:function (res) {
console.log(res);
}
})
PHP
$client_id = $_POST['client_val'];
$row_subClient = $conn->query("SELECT `id`,`name` from `subClient` where `client_id`=$client_id");
$sub_client_arr = [];
while($result_client = $row_subClient->fetch_assoc()){
$sub_client_arr[$result_client['id']] = $result_client['name'];
}
echo json_encode($sub_client_arr);
沒有任何問題here.After獲得在PHP中,我從數據庫中選擇我的數據。這裏也沒有問題。但是,當我做json_encode($ array)和console.log(res)到javascript.It給我所有的html頁面與我的陣列。我的問題,我得到的HTML?請幫助我。提前感謝您。
您可以張貼到了,問題PHP? –
很難說沒有看到../../works/addWork.php – mplungjan
$ client_id = $ _POST ['client_val']; $ row_subClient = $ conn-> query(「SELECT'id','''''subClient''' where'client_id' = $ client_id」); $ sub_client_arr = []; while($ result_client = $ row_subClient-> fetch_assoc()){ $ sub_client_arr [$ result_client ['id']] = $ result_client ['name']; } echo json_encode($ sub_client_arr); –