jquery – Ajax成功和错误功能失败

前端之家收集整理的这篇文章主要介绍了jquery – Ajax成功和错误功能失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法使我的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(/[-[\]{}()*+?.,"\\$&");
            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(); 
?>

解决方法

尝试这个:
$.ajax({
    beforeSend: function() { textreplace(description); },success: function(){  
        $("form#updatejob").hide(function(){$("div.success").fadeIn();});  
    },errorThrown) { 
        alert("Status: " + textStatus); alert("Error: " + errorThrown); 
    }       
});

beforeSend属性设置为function(){textreplace(description); }而不是textreplace(description). beforeSend属性需要一个函数.

猜你在找的jQuery相关文章