2015-08-14 79 views
1

無論代碼是否失敗,我的代碼都會一直返回成功。我嘗試使用不同的方法,但我很難過。我無法讓php向jquery返回一個錯誤ajax

這裏是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(); 
    } 

這裏是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 
       }); 
      } 

我無法設置頁眉過去的內容類型,所以我想只是打印錯誤消息時失敗的東西有時作品但大多沒有。我不知道爲什麼。

+0

您是否試圖訪問您在PHP數組中定義的錯誤消息,並以JSON的形式將其打印回ajax調用? – showdev

回答

3

如果您希望jQuery將響應識別爲錯誤,則需要在PHP中設置HTTP錯誤代碼(例如500)。這可以通過設置一個頭,像這樣做:

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

因爲你的PHP函數返回一個200 HTTP狀態代碼,jQuery將始終認爲這是成功的。這也是處理錯誤的可接受的方式,但是您必須檢查'dataResponse'是否存在您設置的錯誤字段,並將錯誤處理代碼移至成功塊。

+0

有關進一步的參考,請參閱[可能的textStatus值](http://stackoverflow.com/questions/11103995/what-are-all-of-the-possible-values-of-the-textstatus-parameter-in-the -回電話)。 – showdev

+0

當我試圖說我得到一個錯誤,說我不能,因爲「頭已經發送」。 – user3314802

+1

@ user3314802「已發送的標題」表示您已經向瀏覽器發送了一些內容。檢查並確保在嘗試之前不要回顯或打印。 – snollygolly