我有一个像这样的问题。
<select onchange="select()" id="select">
<option value="1">test 1</option>
<option value="2">test 2</option>
</select>
我使用javascript获取#select的值,并使用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);
在php中没有任何问题,我从数据库中选择my datas.There is get too.But when I do json_encode($array),并且console.log(res) to javascript.It用我的arrays.Where返回所有的html页面是我的问题,是我用html得到的吗?请提前帮你me.Thank一下。
发布于 2017-08-02 01:13:22
因此,您添加了dataType:' JSON‘,它需要来自addwork.php文件的JSON数据。
因此,在你的adwork.php文件中,如果你有一个数组数据,那么你需要做一个json数据,这可以用json_encode在php中完成。
$arrayData = array(
'key' => 'value',
'key' => 'value',
'key' => 'value',
'key' => 'value'
);
To encode this in php you can use
header('Content-Type: application/json');
return json_encode($arrayData);
好的,正如谢尔盖所说的那样,另一个问题可能是在你的php文件中你所做的事情
echo json_encode($arraDetails);
<html content below here>
你可以通过以下方式克服那个问题:
echo json_encode($arraDetails);
exit; // This will make sure that the below content won't execute
<html content below here>
发布于 2017-08-02 01:08:12
在后端打印json后,尝试放入exit;。HTML由后台发送。所以问题肯定是存在的。
https://stackoverflow.com/questions/45443807
复制相似问题