ajax后台返回指定的错误码

js:

$.ajax({
            type: "POST",
            url: 'post.php',
            data: serialNumber + "&getSerialNumber=" + 1,
            dataType: 'json',
            success: function(data, textStatus, jqXHR) {
                console.log("SUCCESS");
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("ERROR");
            }
        });

php:

if($product->GetSerial($serial)) {
    $productInfo = $product->GetSerial($serial)->first();
    header('Content-Type: application/json', true, 200);
    die(json_encode(["error"=> false, "message"=> "Successfully got serial number"]));
} else {
    header('Content-Type: application/json', true, 400);
    die(json_encode(["error"=> true, "message"=> "failed to get serial number"]));
}
header('Content-Type: application/json', true, 200);

可以返回规定的错误码。

posted @ 2017-12-01 09:21  Basu  阅读(753)  评论(0编辑  收藏  举报