2012-01-18 106 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"}, 
...