阿贾克斯成功和错误功能衰竭 [英] Ajax Success and Error function failure

查看:136
本文介绍了阿贾克斯成功和错误功能衰竭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的jQuery AJAX的正常工作。它引导到PHP页面来更新数据库,但从未返回到脚本的成功或错误的选择。

我的code是如下:

  $(文件)。就绪(函数(){
        $(#形式updatejob)。递交(函数(){
            功能textreplace(X){返回text.replace(/[-[\]{}()*+?.,\\^$ |#\ S] /克,\\ $&安培;);}
            //我们要存储的表单输入框中的值,然后发送通过ajax以下
            VAR工作= $(#工作)ATTR(价值)。
            。Var描述= $(#说明)VAL();
            description.replace(/[-[\]{}()*+?.,\\^$ |#\ S] /克,\\ $&安培;);
            VAR的startDate = $(#的startDate)ATTR(价值)。
            VAR RELEASEDATE = $(#RELEASEDATE)ATTR(价值)。
            VAR状态= $(#状态)ATTR(价值)。
            $阿贾克斯({
                beforeSend:textreplace(介绍),
                键入:POST,
                网址:updatedjob.php
                数据:作业ID =+工作+&放大器;说明=+说明书+&放大器;的startDate =+的startDate +&放大器; RELEASEDATE =+ RELEASEDATE +&放大器;状态=+状态,
                成功:函数(){
                    $(#形式updatejob)隐藏(函数(){$(div.success)淡入();});
                },
                错误:函数(XMLHtt prequest,textStatus,errorThrown){
                    警报(状态:+ textStatus);警报(错误:+ errorThrown);
                }
            });
            返回false;
        });
});
 

和PHP的:

 < PHP
    包括(将connect.php);
    $就业=修剪($ _ POST ['工作']);
    $的startDate =修剪($ _ POST ['的startDate']);
    $ RELEASEDATE =修剪($ _ POST ['RELEASEDATE']);
    $ mysqlstartdate =日期(YM-D时的strtotime($的startDate));
    $ mysqlreleasedate =日期(YM-D时的strtotime($ RELEASEDATE));
    $描述=修剪($ _ POST ['介绍']);
    $状态=修剪($ _ POST ['状态']);
    $更新=更新作业SET的startDate ='$ mysqlstartdate',RELEASEDATE ='$ mysqlreleasedate',说明='$说明,状态='$状态'WHERE作业ID ='$工作';
    $ rsUpdate =请求mysql_query($更新);
//或死亡(mysql_error());则mysql_close();
?>
 

解决方案

试试这个:

  $。阿贾克斯({
    beforeSend:函数(){textreplace(介绍); },
    键入:POST,
    网址:updatedjob.php
    数据:作业ID =+工作+&放大器;说明=+说明书+&放大器;的startDate =+的startDate +&放大器; RELEASEDATE =+ RELEASEDATE +&放大器;状态=+状态,
    成功:函数(){
        $(#形式updatejob)隐藏(函数(){$(div.success)淡入();});
    },
    错误:函数(XMLHtt prequest,textStatus,errorThrown){
        警报(状态:+ textStatus);警报(错误:+ errorThrown);
    }
});
 

beforeSend 属性设置为函数(){textreplace(介绍); } 而不是 textreplace(介绍)。该 beforeSend 属性需要的功能。

I am having trouble getting my jQuery ajax to work properly. It directs to the PHP page to update the database, but never returns back to the script for the success or error options.

My code is below:

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

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

解决方案

Try this:

$.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); 
    }       
});

The beforeSend property is set to function() { textreplace(description); } instead of textreplace(description). The beforeSend property needs a function.

这篇关于阿贾克斯成功和错误功能衰竭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆