2017-02-13 155 views
1

當我按下具有某些功能的按鈕(在php代碼中)時,我不明白爲什麼我有時會收到錯誤,有時會從ajax中獲得成功。 我還有一個按鈕,其底部的代碼結構幾乎相同。所以,我有2個相同結構的ajax代碼(但變量或文件的名稱不同)。這很重要嗎?Ajax中的POST請求錯誤。錯誤後的空白頁面

阿賈克斯:

function plan_tarifar_submit_anulare() { 
    var id = get_user_id_anulare(); 
    var myURL = "./UpdateAnularePlanTarifar.php"; 
    $.ajax({ 
     timeout: 5000, 
     type: "POST", 
     url: myURL, 
     data: {id:id}, 
     error: function(xhr) { 
      alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' -> ' + xhr.responseText); 
      parent.location.reload(); 

     },    
     success: function (xhr) { 
      alert('IT WORKS !!' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText); 
      parent.location.reload(); 
     } 
    }); 
    return false; 
} 

PHP:

 <?php 
    /** 
    * Description of updateAnularePlanTarifar (...) 
    */ 
    require_once("../includes.inc.php"); 
    $cookie_value = assure_login_check(); 

    if (!isset($_POST['id'])) { 
     return; 
    } 
    $user_id = htmlspecialchars($_POST['id']); 
function getOldIstoric($id){ 
    (.....) 
    return $istoric; 
} 
function anularePlanTarifar($id) { 
    (.....) 
} 
anularePlanTarifar($user_id); 
?> 

而且,錯誤後的頁面無法刷新: unloaded page

我認爲ID是在失蹤網址結束。

+0

如果問題只是間歇性的,那很可能是服務器問題。當請求未能查看它是否包含任何錯誤消息時,是否檢查過響應?另外請注意,在發出AJAX請求後重新加載頁面會使請求變得冗餘。 –

+0

這不是一個有效的URL:「./UpdateAnularePlanTarifar.php」這是一個文件路徑,但不是你可以用作URL的東西。一個URL看起來像:{host}/{path} /UpdateAnularePlanTarifar.php或者只是「/UpdateAnularePlanTarifar.php」,如果該文件位於web根目錄 – Auris

+2

@Auris那根本不是真的。 '。/'是一個完全有效的路徑結構:http://stackoverflow.com/questions/7591240/what-does-dot-slash-refer-to-in-terms-of-an-html-file-path-location –

回答

0

取決於您的html代碼的樣子。 但我想象在表單標籤中有東西。

檢查是否已爲表單標籤的屬性:

<form action="#" method="post" onclick='plan_tarifar_submit_anulare();' id='yourID'> <!--something--> </form>

它應該是這樣的或類似的。

讓我知道它是否有效。如果你仍然有問題,請顯示你的html代碼。