Php 将参数从$.ajax post传递到mysqli\u查询不起作用
我试图将参数从$.ajax post传递到mysqli\u查询 这是我的ajaxPhp 将参数从$.ajax post传递到mysqli\u查询不起作用,php,jquery,ajax,Php,Jquery,Ajax,我试图将参数从$.ajax post传递到mysqli\u查询 这是我的ajax $.ajax({ type: 'post', url: 'edit-doctor.php', data: "imei="+imei, contentType: "application/json; charset=utf-8", dataType: "json", succe
$.ajax({
type: 'post',
url: 'edit-doctor.php',
data: "imei="+imei,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
$.each(data, function() {
$.each(this, function(k , v) {
trHTML += '<tr><td><b>'+ k.toString() + '</b></td> : <td>' + v.toString() + '</td></tr>';
})
})
$("#target_table_id").append(trHTML);
}
});
$.ajax({
键入:“post”,
url:“编辑医生.php”,
数据:“imei=“+imei,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
警报(数据);
$.each(数据,函数(){
$。每个(此,函数(k,v){
trHTML+=''+k.toString()+':'+v.toString()+'';
})
})
$(“#target_table_id”).append(trHTML);
}
});
这是我的php
<?php
include("connect.php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$src1= $_POST["imei"]; //.. getting variable in src1
$sql = "select * from tbl_beacons where imei = '".$src1."' ";
//$sql = "select * from tbl_beacons where imei = '".$_POST['imei']."' ";
// also tried this
$result = $conn->query($sql);
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
$conn->close();
数据:“imei=”+imei,不是JSON。尝试数据:{imei:imei},
如果发送JSON,也需要接收JSON
您也可以只删除contentType:“application/json;charset=utf-8”,数据类型:“json”,
更好的是:
$.post('edit-doctor.php', {"imei":imei},function (data) {
var trHTML=[];
$.each(data, function() {
$.each(this, function(k , v) {
trHTML.push('<tr><td><b>'+ k + '</b></td> : <td>' + v + '</td></tr>');
})
})
$("#target_table_id").append(trHTML.join(''));
});
$.post('edit-doctor.php',{“imei”:imei},函数(数据){
var trHTML=[];
$.each(数据,函数(){
$。每个(此,函数(k,v){
trHTML.push(“+k+”:“+v+”);
})
})
$(“#target_table_id”).append(trHTML.join(“”));
});
我认为这是我的答案,这是将数据从js发送到php的最简单的方法,但是如果你写一些大/强大/用户的东西,这种方法还不够强大friendly@J.WCT-这是什么意思,不够强大?如果您的意思是没有错误处理,那么只需使用原始的$.ajax
$.post('edit-doctor.php', {"imei":imei},function (data) {
var trHTML=[];
$.each(data, function() {
$.each(this, function(k , v) {
trHTML.push('<tr><td><b>'+ k + '</b></td> : <td>' + v + '</td></tr>');
})
})
$("#target_table_id").append(trHTML.join(''));
});