Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php向sweetalert返回错误_Php_Ajax_Sweetalert - Fatal编程技术网

从php向sweetalert返回错误

从php向sweetalert返回错误,php,ajax,sweetalert,Php,Ajax,Sweetalert,我有这个ajax代码来处理php文件 $.ajax({ url:'vals/Package.php', data:$(this).serialize(), type:'POST', success:function(data){ console.log(data); swal("Success", "We will contact you soon! Thank you :)", "success"); }, error:function(data){

我有这个ajax代码来处理php文件

$.ajax({
  url:'vals/Package.php', 
  data:$(this).serialize(),
  type:'POST',
  success:function(data){
    console.log(data);
    swal("Success", "We will contact you soon! Thank you :)", "success");
  },
  error:function(data){
    swal("Sorry", "Failed to send order. Please try later :(", "error");
  }
});
在my Package.php中

$Query = mysqli_query($connecDB,"insert into query here");
if($Query){
    echo 'success message';
}
else{
    echo 'error here';
}
try {
    $Query = mysqli_query($connecDB, "insert into query here");
    if ($Query) {
        echo 'success message';
    } else {
        throw new Exception('dummy error message');
    }
} catch (Exception $exc) {   
    //handle any errors in your code, including connection issues
    echo 'error'; //this will be your "flag" to handle on the client side
    //and if you want, can also log the error with 
    //$exc->getMessage() or $exc->getTraceAsString()
    die;
}
如果在数据库中插入时出错,如何将其发送到sweetalert


如果我删除if-else条件,它将显示成功消息。我还试图显示错误消息。

您现在只是回显一个错误字符串,但该响应仍将以状态200标头发送。您需要做的是显式设置标题:

header('HTTP/1.1 500 Internal Server Error');
echo 'error message';

我认为jQuery只是将所有非HTTP200响应视为错误(即不成功的请求)是正确的。可能会有一些例外,比如404、301或201状态码。无论哪种方式,如果您想指出一个严重的问题:使用http 500(内部服务器错误)

您现在只需回显一个错误字符串,但该响应仍将以status 200报头发送。您需要做的是显式设置标题:

header('HTTP/1.1 500 Internal Server Error');
echo 'error message';

我认为jQuery只是将所有非HTTP200响应视为错误(即不成功的请求)是正确的。可能会有一些例外,比如404、301或201状态码。无论哪种方式,如果您想指出一个严重的问题:使用http 500(内部服务器错误)

最好的解决方案是使用并发送两种消息作为响应,下面是使用您自己的代码截取的一个:

在您的package.php中

$Query = mysqli_query($connecDB,"insert into query here");
if($Query){
    echo 'success message';
}
else{
    echo 'error here';
}
try {
    $Query = mysqli_query($connecDB, "insert into query here");
    if ($Query) {
        echo 'success message';
    } else {
        throw new Exception('dummy error message');
    }
} catch (Exception $exc) {   
    //handle any errors in your code, including connection issues
    echo 'error'; //this will be your "flag" to handle on the client side
    //and if you want, can also log the error with 
    //$exc->getMessage() or $exc->getTraceAsString()
    die;
}
在JS文件中:

$.ajax({
    url: 'vals/Package.php',
    data: $(this).serialize(),
    type: 'POST',
    success: function (data) {
        if (data !== 'error') {
            swal("Success", "We will contact you soon! Thank you :)", "success");
        } else {
           //our handled error
            swal("Sorry", "Failed to send order. Please try later :(", "error");
        }
    },
    error: function (data) {
           //other errors that we didn't handle
        swal("Sorry", "Failed to send order. Please try later :(", "error");
    }
});
通过使用您的代码,您将能够防弹防止意外错误。
注意:当然,您可以编辑标题,而不是回显您自己的标志,并返回一个http错误,该错误将在ajax错误函数中处理。

最佳解决方案是使用a并发送两种消息作为响应,以下是使用您自己的代码截取的消息:

在您的package.php中

$Query = mysqli_query($connecDB,"insert into query here");
if($Query){
    echo 'success message';
}
else{
    echo 'error here';
}
try {
    $Query = mysqli_query($connecDB, "insert into query here");
    if ($Query) {
        echo 'success message';
    } else {
        throw new Exception('dummy error message');
    }
} catch (Exception $exc) {   
    //handle any errors in your code, including connection issues
    echo 'error'; //this will be your "flag" to handle on the client side
    //and if you want, can also log the error with 
    //$exc->getMessage() or $exc->getTraceAsString()
    die;
}
在JS文件中:

$.ajax({
    url: 'vals/Package.php',
    data: $(this).serialize(),
    type: 'POST',
    success: function (data) {
        if (data !== 'error') {
            swal("Success", "We will contact you soon! Thank you :)", "success");
        } else {
           //our handled error
            swal("Sorry", "Failed to send order. Please try later :(", "error");
        }
    },
    error: function (data) {
           //other errors that we didn't handle
        swal("Sorry", "Failed to send order. Please try later :(", "error");
    }
});
通过使用您的代码,您将能够防弹防止意外错误。
注意:当然,您可以编辑标题而不是回显您自己的标志,并返回一个http错误,该错误将在ajax错误函数中处理。

我发现了这段代码,它似乎可以工作

if($Query){

}
else{
    header('HTTP/1.1 500 Internal Server...');
    header('Content-Type: application/json; charset=UTF-8');
    die(json_encode(array('message' => 'ERROR', 'code' => 1337)));
}

我发现了这个代码,它似乎在工作

if($Query){

}
else{
    header('HTTP/1.1 500 Internal Server...');
    header('Content-Type: application/json; charset=UTF-8');
    die(json_encode(array('message' => 'ERROR', 'code' => 1337)));
}

我通常从php返回一个json字符串,其中包含状态消息的
msg
err
键,有时还返回一个
success
布尔值。然后我可以在json返回中查找适当的键,如果设置了,
alert(result.msg)
…等等。我通常从php返回json字符串,其中包含状态消息的键,如
msg
err
,有时还包含
success
布尔值。然后我可以在json返回中查找适当的键,如果设置了,
alert(result.msg)
…等等。