【发布时间】:2022-11-11 14:48:47
【问题描述】:
嘿,我使用 PHP 编写了一个用于插入数据的 rest api。我试图将消息呈现到我的 ajax 成功响应中,但我没有得到任何响应,而是一堆整个对象。
我的 php 返回码
if(mysqli_query($connection , $ins)){
echo json_encode(array('message' => 'Item Added' , 'status' => true));
}else{
echo json_encode(array('message' => 'Failed to add item' , 'status' => false));
}
这是我的ajax调用
$.ajax({
url : "http://localhost/cokeinventory/rest-api/api-insert-item.php",
type : "POST",
data : JSON.stringify({
item:itemname , unit:unit , date:expdate , bcode:bcode , blabel:blabel
}),
error:err=>{
console.log(err)
},
success:function(data){
console.log(data.message);
}
});
});
【问题讨论】:
-
if(mysqli_query($connection, $ins)){ echo json_encode(array('message' => 'Item added' , 'status' => true));出口(); }else{ echo json_encode(array('message' => '添加项目失败' , 'status' => false)); }
-
这个问题与 HTML 有什么关系?
-
你对这段代码有什么疑问?这是 PHP 问题,还是 jQuery 问题?请删除与您的问题无关的标签