2011-03-11 47 views
2

夥計們,我有一個jQuery頁面與Ajax,並提交在一個單獨的PHP文件,沒有用戶界面。所以我想要的是,如果說例如我的PHP文件中的插入查詢將失敗,我的PHP中的回聲(您的插入失敗)將在我的jquery頁面中發出警報。怎麼做?如何提醒ajax返回的數據?

像這樣的警報(數據);

+0

我想你真正想說的是:「如何從PHP返回關於錯誤的數據並將其顯示在頁面上」。我對嗎? – 2011-03-11 11:32:54

+0

我編輯了我的答案,希望這是你正在尋找的 – Greg 2011-03-11 12:17:52

回答

-1

嘗試這樣

$.ajax({ 
    url: "/post/post_url.php", 
    type: "POST", 
    data: parameters, 
    success: function(){ 
    alert('success'); 
    }, 
    error: function(){ 
    alert('failure'); 
    } 
}); 

Reference

+2

我認爲是不正確的,因爲腳本PHP執行沒有錯誤(邏輯有錯誤,而不是過程),那麼返回代碼將永遠是'成功」。 – Akarun 2011-03-11 11:24:00

+0

您可以向成功函數'success:function(data){...}'添加一個參數,它保存來自服務器的響應。然後你的函數可以包含'alert(data);'來顯示服務器的實際響應。 – mdm 2011-03-11 11:24:57

-1

您可以使用success處理程序$.ajax呼叫作爲diEcho寫道。 在這個處理程序的內部,您可以通過某些標誌來決定您的PHP操作是成功還是失敗。

error$.ajax的處理程序更可能發生ajax調用失敗,而不是請求操作的狀態。

編輯:

通過diEcho適用於例如:

$.ajax({ 
    url: "/post/post_url.php", 
    type: "POST", 
    data: parameters, 
    success: function(data){ 
    alert('ajax call finished successfully'); 
    if (!data.insertedOk) { 
     alert(data.message); 
    } else { 
     // insert succeeded 
    } 
    }, 
    error: function(){ 
    alert('ajax call failure'); 
    // this mean, /post/post_url.php calling failed (file not found etc...) 
    } 
}); 

EDIT2:

在PHP中,你可以使用以下

if ($queryExecutedSuccessfully) { 
    $return['insertedOk'] = true; 
    $return['message'] = 'Your success message'; 
} else { 
    $return['insertedOk'] = false; 
    $return['message'] = 'Error message when insert fails.'; 
} 
echo json_encode($return); 
+0

你從哪裏得到insertd0k?它是一個PHP頁面中的變量? – user628961 2011-03-11 11:44:06

+0

所有我想要做的就是讓我的回聲在PHP提醒我在其他jQuery頁面ajax成功。 – user628961 2011-03-11 11:45:57

+0

檢查EDIT2中的答案 – rdamborsky 2011-03-11 11:55:43

-1

關於@diEcho的代碼,你還需要在success: function()測試您的返回的答案與狀態代碼(wrot e由PHP)。

另一方面,您可以通過發送一個「Error 500」標題來強制PHP腳本中的錯誤,該標題生成「Fail」回調。 但這不是一個好方法!

在你的PHP:

if (true === $QueryHasFail) { 
    header('HTTP/1.1 500 Internal Server Error'); // <--- Ajax Callback error: function() 
    echo "Oupss, somethink wrong!"; 
} else { 
    echo "Yeah, great"; // <--- Ajax Callback success: function() 
} 
exit(0); 

點擊此處瞭解詳情:http://www.rachaelarnold.com/dev/archive/trigger-ajax-error-event

+0

你說什麼?請用代碼 – diEcho 2011-03-11 11:28:58

+0

解釋我已經添加了「500header」方法的示例 – Akarun 2011-03-11 11:37:28

4

編輯2:

警報任何PHP回聲:

function get_data() { 
    $.ajax({ 
     url: 'get_data.php?rand=' + Math.random(), 
     type: 'GET' 
     success: function(results) { 
      alert(results); 
     } 
    }); 
} 

編輯1:

如果你想在錯誤出現在警告,這樣做:

調試AJAX,你可以檢查XHR,狀態和錯誤,像這樣:

function get_data() { 
    $.ajax({ 
     url: 'get_data.php?rand=' + Math.random(), 
     type: 'GET', 
     error: function(xhr, status, error) { 
      alert(status); 
      alert(xhr.responseText); 
     }, 
     success: function(results) { 
      /* clear old results, then display the new results */ 
      $("#divResults").empty().append(results); 
     } 
    }); 
} 

但這可能並不總是顯示完整的消息,特別是如果錯誤消息包含大量數據。它可能最終會脫離屏幕。

原來的答案:

調試AJAX,你可以檢查XHR,狀態和錯誤,像這樣:

function get_data() { 
    $.ajax({ 
     url: 'get_data.php?rand=' + Math.random(), 
     type: 'GET', 
     error: function(xhr, status, error) { 
      /* clear old error message, then display the new php error message */ 
      $("#divErrorMessages").empty().append(status); 
      $("#divErrorMessages").append(xhr.responseText); 
     }, 
     success: function(results) { 
      /* clear the error message first */ 
      $("#divErrorMessages").empty(); 

      /* clear old results, then display the new results */ 
      $("#divResults").empty().append(results); 
     } 
    }); 
} 

在你的HTML你應該有2周的div

<div id="divResults"></div> 
<div id="divErrorMessages"></div> 
+0

我希望它僅顯示爲警報。不在div – user628961 2011-03-11 12:34:57

+0

,我不提醒錯誤。我正在提醒在PHP中迴應什麼。不一定是錯誤。 – user628961 2011-03-11 12:35:43

+0

如果是這樣的話,那麼你只需要提醒成功函數返回的是什麼。檢查編輯2 – oshirowanen 2011-03-14 15:08:23

-1

爲了提醒你在PHP中回顯什麼,「數據」作爲第一個參數傳入成功函數,然後在報警輸出在這個例子:

$.ajax({ 
    url: "/post/post_url.php", 
    type: "POST", 
    data: parameters, 
    success: function(data){ 
    alert(data); 
    }, 
    error: function(jqXHR, textStatus, errorThrown){ 
    alert('Failure: ' + textStatus + ". Error thrown: " + errorThrown); 
    } 
}); 
+0

什麼是textStatus和jqXHR?我想要做的是警告什麼是在我的PHP回顯。 – user628961 2011-03-11 11:52:56

+0

如果你想這樣做,請檢查我的答案。 – oshirowanen 2011-03-11 12:15:25

+0

我試過了。它可以顯示任何東西。 – user628961 2011-03-11 12:42:16

-1

我知道這是一個老話題,但我發現它尋找類似的東西,我決定添加一些信息,可能對以後的搜索有用。

我認爲溶液到這可能是由aproached發送適當header()函數或http_response_code($ HTTP_ERROR_CODE);在你的PHP腳本,這取決於你的PHP腳本的答案。你可以在這個帖子找到相關信息:

How to make a ajax call to a php page a success or error?

How to send a server error response using php?

這取決於PHP的版本使用的是您選擇(頁眉或http_response_code)功能,似乎http_response_code是可從PHP 5.4獲得。

一個解決辦法,以避免「PHP版本的交叉編譯」可能會被編碼在測試它在故障實存結果的情況下自己http_response_code來實現,如在php.net這等後建議:

http://php.net/manual/en/function.http-response-code.php在您的HTML/jQuery文檔中,您可以檢查您的request.fail,request.done和request.always以檢索您的PHP腳本提供的信息,以便在您方便時使用它。

希望這會有所幫助。