【问题标题】:Ajax Success and Error function failureAjax 成功和错误函数失败
【发布时间】:2012-01-18 22:10:57
【问题描述】:

我无法让我的 jQuery ajax 正常工作。它指向 PHP 页面以更新数据库,但从不返回脚本以获得成功或错误选项。

我的代码如下:

$(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(); 
?>

【问题讨论】:

  • 如果你在success回调函数的第一行加上alert()会发生什么? success: function(){ alert('foobar');...
  • 您也提供 php 代码似乎是合乎逻辑的。你在回应吗?
  • 这里是 PHP:&lt;?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(); ?&gt;
  • Jasper:我的成功永远不会运行,因为 PHP 页面永远不会返回到 javascript。
  • 如果您将 php 放入问题中(并且格式清晰),您会更快得到答案

标签: jquery ajax


【解决方案1】:

试试这个:

$.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 属性需要一个函数。

【讨论】:

  • 我改变了那部分,但它仍然没有返回函数。感谢您的尝试。
【解决方案2】:

也可以使用以下方法来捕获错误:

$.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 调用中添加一个 dataType = "text" 行来解决它。使 dataType 与您期望从服务器返回的响应相匹配(您的“插入成功”或“出现问题”错误消息)。

    【讨论】:

      【解决方案4】:

      您可以按如下方式实现特定于错误的逻辑:

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

      【讨论】:

        【解决方案5】:

        这可能是一篇旧帖子,但我意识到 php 没有返回任何内容,并且您的成功函数没有如下输入,success:function(e){}。希望对你有帮助。

        【讨论】:

          【解决方案6】:

          这可能无法解决您的所有问题,但您在函数中使用的变量(文本)与您传入的参数 (x) 不同。

          变化:

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

          收件人:

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

          似乎会有一些好处。

          【讨论】:

            【解决方案7】:

            您正在发送一个帖子类型,其中包含为获取实现的数据。 您的表单必须如下:

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

            【讨论】:

              【解决方案8】:
               $.ajax({
                       type:'POST',
                       url: 'ajaxRequest.php',
                       data:{
                       userEmail : userEmail
                       },
                       success:function(data){
                       if(data == "error"){
                              $('#ShowError').show().text("Email dosen't Match ");
                              $('#ShowSuccess').hide();
                             }
                             else{
                               $('#ShowSuccess').show().text(data);
                              }
                            }
                          });
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-09-12
                • 2023-03-07
                • 2016-10-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-01-13
                • 1970-01-01
                相关资源
                最近更新 更多