2012-01-18 169 views
19

我無法讓我的jQuery Ajax正常工作。它指向PHP頁面來更新數據庫,但不會返回成功或錯誤選項的腳本。Ajax成功和錯誤功能失敗

我的代碼如下:

$(document).ready(function(){ 
     $("form#updatejob").submit(function() { 
      function textreplace(x) {return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");} 
      // we want to store the values from the form input box, then send via ajax below 
      var job  = $("#job").attr("value"); 
      var description  = $("#description").val(); 
      description.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
      var startDate = $("#startDate").attr("value"); 
      var releaseDate = $("#releaseDate").attr("value"); 
      var status = $("#status").attr("value"); 
      $.ajax({ 
       beforeSend:textreplace(description), 
       type: "POST", 
       url: "updatedjob.php", 
       data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
       success: function(){ 
        $("form#updatejob").hide(function(){$("div.success").fadeIn();}); 
       }, 
       error: function(XMLHttpRequest, textStatus, errorThrown) { 
        alert("Status: " + textStatus); alert("Error: " + errorThrown); 
       }  
      }); 
      return false; 
     }); 
}); 

而且PHP:

<?php 
    include("connect.php"); 
    $job = trim($_POST['job']); 
    $startDate = trim($_POST['startDate']); 
    $releaseDate = trim($_POST['releaseDate']); 
    $mysqlstartdate = date('Y-m-d', strtotime($startDate)); 
    $mysqlreleasedate = date('Y-m-d', strtotime($releaseDate)); 
    $description = trim($_POST['description']); 
    $status = trim($_POST['status']); 
    $update = "UPDATE jobs SET startDate = '$mysqlstartdate', releaseDate = '$mysqlreleasedate', description = '$description', status = '$status' WHERE jobID = '$job' "; 
    $rsUpdate = mysql_query($update); 
// or die(mysql_error()); mysql_close(); 
?> 
+0

會發生什麼,如果你把一個'警報()'的'success'回調函數的第一行? '成功:function(){alert('foobar'); ...' – Jasper 2012-01-18 22:14:48

+0

您提供php代碼似乎也是合乎邏輯的。你是否迴應迴應? – 2012-01-18 22:18:55

+0

這裏是PHP:'<?php include(「connect。(); $ job = trim($ _ POST ['releaseDate']); $ job = trim($ _ POST ['job']); $ startDate = trim $ mysqlstartdate = date('ym -d',strtotime($ startDate)); $ mysqlreleasedate = date('Ym-d',strtotime($ releaseDate)); $ description = trim($ _ POST ['description'] ); $ status = trim($ _ POST ['status']); $ update =「UPDATE jobs SET startDate ='$ mysqlstartdate',releaseDate ='$ mysqlreleasedate',description ='$ description',status ='$狀態'WHERE jobID ='$ job'「; $ rsUpdate = mysql_query($ update); // or die(mysql_error()); mysql_close(); ?>' – michael 2012-01-19 16:46:41

回答

48

試試這個:

$.ajax({ 
    beforeSend: function() { textreplace(description); }, 
    type: "POST", 
    url: "updatedjob.php", 
    data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
    success: function(){ 
     $("form#updatejob").hide(function(){$("div.success").fadeIn();}); 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     alert("Status: " + textStatus); alert("Error: " + errorThrown); 
    }  
}); 

beforeSend屬性設置爲function() { textreplace(description); }而不是textreplace(description)beforeSend屬性需要一個函數。

+0

我改變了這個部分,但它仍然沒有返回到函數。謝謝你的嘗試。 – michael 2012-01-19 16:36:15

0

這可能無法解決您的所有問題,但您在函數(文本)中使用的變量與您在(x)中傳遞的參數不同。

更改:

function textreplace(x) { 
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
} 

要:

function textreplace(text) { 
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
} 

好像它會做一些很好的。

3

您可以實現特定的錯誤邏輯如下:

error: function (XMLHttpRequest, textStatus, errorThrown) { 
    if (textStatus == 'Unauthorized') { 
     alert('custom message. Error: ' + errorThrown); 
    } else { 
     alert('custom message. Error: ' + errorThrown); 
    } 
} 
2

這可能是舊的文章,但我意識到有什麼可從PHP回來,你的成功的功能沒有輸入類似如下,success:function(e){}。我希望這可以幫助你。

4

你也可以使用以下方法來捕捉錯誤:

$.ajax({ 
    url: url, 
    success: function (data) { 
     // Handle success here 
     $('#editor-content-container').html(data); 
     $('#editor-container').modal('show'); 
    }, 
    cache: false 
}).fail(function (jqXHR, textStatus, error) { 
    // Handle error here 
    $('#editor-content-container').html(jqXHR.responseText); 
    $('#editor-container').modal('show'); 
}); 
3

我有同樣的問題,並通過簡單地增加一個數據類型=「文本」行到我的Ajax調用固定它。使數據類型與您希望從服務器獲取的響應相匹配(您的「插入成功」或「出錯的錯誤」錯誤消息)。

0

您正在發送包含獲取數據的帖子類型。 您的形式必須是以下幾點:

$.ajax({ 
url: url, 
method: "POST", 
data: {data1:"data1",data2:"data2"}, 
...