gpt4 book ai didi

javascript - jQuery PHP POST 错误处理

转载 作者:行者123 更新时间:2023-11-29 10:38:53 25 4
gpt4 key购买 nike

我通过 jQuery 发送 POST 请求:

if ( isset( $_POST['newOwner'], $_POST['uID'] ) ) :
$function->updateTransport( $_POST['newOwner'], $_POST['uID'] );
endif;

$.post("", {
newOwner: $("#newOwner").val(),
uID: uid
})

当 MySQL 无法更新时,如何登录 jQuery?我尝试过 .fail 但 jQuery 总是显示成功的发布请求。

public function updateTransport( $newOwner, $uID )
{
try
{
$stmt = $this->db->prepare( "UPDATE cars SET owner= ? WHERE id = ?" );
$stmt->execute( array( $newOwner, $uID ) );
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}

最佳答案

您需要发送有关执行查询成功和失败的特定数据。试试这个代码。

    public function updateTransport( $newOwner, $uID ){
try
{
$stmt = $this->db->prepare( "UPDATE cars SET owner= ? WHERE id = ?" );
$status = $stmt->execute( array( $newOwner, $uID ) );
if($status){
$ret = array(
'status' => 'success',
'message' => 'Success'
);
echo json_encode($ret);
}else{
$ret = array(
'status' => 'failed',
'message' => 'Failed'
);
echo json_encode($ret);
}
}
catch(PDOException $e)
{
$ret = array(
'status' => 'failed',
'message' => $e->getMessage()
);
echo json_encode($ret);
}
}
$.ajax({
'url' : 'yourURL',
data : { 'key' : value },
success : function(data, textStatus, jqXHR){
var dat = JSON.parse(data);
if(dat.status == 'success'){

}else{

}
},
error : function(jqXHR, textStatus, errorThrown){
var dat = JSON.parse(data);
if(dat.status == 'success'){

}else{

}
}
});

关于javascript - jQuery PHP POST 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45906930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com