我无法让 php 向 jquery ajax 返回错误

use*_*802 4 php ajax error-handling jquery json

无论代码是否失败,我的代码都会不断返回成功。我尝试使用不同的方法,但我被难住了。

这是PHP

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Content-Type: application/json; charset=UTF-8');

if (isset($_POST['username']) && isset($_POST['password']))
{
    if (file_exists("../inc/dbc.php")){
        if(!include('../inc/dbc.php')){
            print(json_encode(array('message' => 'ERROR', 'code' => 404)));
            die();
        } 
    }
    else{
        print(json_encode(array('message' => 'ERROR', 'code' => 404)));
    }
    // store post variables in temp variables
    $temp_user =                    mysql_real_escape_string(stripslashes($_POST['username']));
    $temp_pass =                    mysql_real_escape_string(stripslashes($_POST['password']));

    //Select users that have matching passwords and are currenlty employed
    $query =                        "SELECT afid, logged, active, code FROM employee as emp WHERE (emp.acct = '$temp_user') AND (emp.code = '$temp_pass') AND (emp.active = 'Y') AND ((emp.logged = 'k') OR (emp.logged='K'))"; 

    //Run the query then set the number of results as a query
    $result =                       mysql_query($query); 
    if(!$result){
        $error = mysql_error();
        print(json_encode(array('message' => 'error', 'code' => 500, 'statusText' => $error)));
        die();
    }
Run Code Online (Sandbox Code Playgroud)

这是 JavaScript

function authorize(dataString,usr,pass){

                $.ajax({
                    crossDomain: true,
                    type: 'POST',
                    url: "domain/app/login.php",
                    dataType: 'json',
                    async: 'true',
                    data: dataString,
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    success: function(dataResponse,status,message){
                        if(status == 'success'){
                            localStorage.setItem("pLog",dataResponse[['message']]);
                            localStorage.setItem('username',usr);
                            localStorage.setItem('password',pass);
                            $("#customer-page").off();
                                $.mobile.loading("show");
                                loadCustomers(localStorage.getItem('ezp-username'),localStorage.getItem('pLog'));
                        }
                    },
                    error: function(dataResponse,status,message){
                        gl.error_msg = "Could not log in; check user name and password.";
                        $(".error-msg").html(gl.error_msg).css('display', 'block');
                        ezReset("error_msg");
                        $.mobile.loading("hide");
                    },
                    timeout: 5000
                });
            }
Run Code Online (Sandbox Code Playgroud)

我无法将标题设置为过去的内容类型,因此我尝试在某些失败时仅打印错误消息,这有时有效,但大多数情况下无效。我不知道为什么。

dpe*_*jar 5

如果您希望 jQuery 将响应识别为错误,则需要在 PHP 中设置 HTTP 错误代码(例如 500)。这可以通过像这样设置标题来完成:

header('HTTP/1.1 500 Internal Server Error');
Run Code Online (Sandbox Code Playgroud)

由于您的 PHP 正在返回 200 HTTP 状态代码,jQuery 将始终将其视为成功。这也是处理错误的一种可接受的方式,但您必须检查您的“dataResponse”是否存在您设置的错误字段并将错误处理代码移至成功块。