2016-08-19 42 views
0

我嘗試使用下面的情景測試錯誤回調觸發錯誤回調jQuery的AJAX:無法使用PHP

  1. 我的網址更改:「telemetry.php」到URL:「asdasfjgafas.php」
  2. <?php header("HTTP/1.1 404 Not Found"); exit() >內telemetry.php

但是,我沒有得到警報()錯誤回調,甚至嘗試1和2分別後!

我使用jQuery的.js內容製作AJAX請求:

$.ajax({ 

    url  : "telemetry.php", 
    data : ({ 
        DshBrdName : strFullDashBoardName, 
        DshBrdID : currentDashboard, 
        r   : Math.random() 
       }), 
    success : function(data, textStatus, jQxhr){ 
        alert(textStatus); 
       }, 
    error : function(jqXHR, textStatus, errorThrown){ 
        alert(textStatus); 
        alert(errorThrown); 
       }, 
    type : "POST" 
}); 

P.S:我能夠獲得成功的警報時,我有合法的URL和有效的PHP代碼!

回答

1

我可以在你的錯誤塊看到一個錯字:

function(jqXHR, textStatus, errorThrown){ 
       alert(testStatus); // should be textStatus? 
       alert(errorThrown); 
      }, 

編輯

從JS的角度來看,似乎一切都ok了,下面你片斷(我更換了未定義的變量與字符串)警報錯誤(因爲這個PHP腳本顯然沒有找到)。你可能想提供有關在服務器端發生的事情更詳細...

$.ajax({ 
 

 
    url  : "telemetry.php", 
 
    data : ({ 
 
        DshBrdName : 'strFullDashBoardName', 
 
        DshBrdID : 'currentDashboard', 
 
        r   : Math.random() 
 
       }), 
 
    success : function(data, textStatus, jQxhr){ 
 
        alert(textStatus); 
 
       }, 
 
    error : function(jqXHR, textStatus, errorThrown){ 
 
        alert(textStatus); 
 
        alert(errorThrown); 
 
       }, 
 
    type : "POST" 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

是你可能在控制檯錯誤,指出'testStatus'是不確定的。 – jcubic

+0

對不起傢伙..我手動輸入這些警報在stackoverflow頁面,而不是從我的PHP文件複製...這不是一個錯字問題!在真正的代碼中,確實是alert(textStatus) –

+0

在這裏運行你的腳本會得到警報,所以問題可能不會出現在javascript中。 – kennasoft