ajax成功错误函数不会在jquery中调用

时间:2013-09-10 04:39:54

标签: javascript ajax jquery

嗨,这是我的代码..

<html>
<head>   
<script src="http://code.jquery.com/jquery-1.8.0.min.js">
</script>
</head>  
<body> 
<form id="foo">

    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />

</form>  
<div id ="onsuccess">      
</div>   
<script>  
// variable to hold request
var request;
// bind to the submit event of our form
$("#foo").submit(function(event){   
    var $form = $(this);
    var serializedData = $form.serialize();
    request = $.ajax({
        url: "a.txt",
        type: "post",
        dataType: "text", 
        data: serializedData,
        success: function(){ 
        console.log("yes, its successful");},
        error : function(){console.log("got an error");}
    });
});

</script>
</body> 
</html>

我正在尝试访问位于同一目录中的a.txt。但是成功和错误函数永远不会调用,这是不可理解的。 Firebug net状态显示没有调用.strange

然而,如果我使用

request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");
        //$("#success").html(response);
    });

确实有效。

2 个答案:

答案 0 :(得分:1)

需要添加此项以提交处理程序

event.preventDefault()

在IIS上将POST更改为GET也适用于我

答案 1 :(得分:0)

尝试.done,.fail和.always

jquery 1.8中的成功和错误回调是deprecated

  

弃用通知:jqXHR.success(),jqXHR.error()和   从jQuery 1.8开始,不推荐使用jqXHR.complete()回调函数。准备   你的代码最终删除,使用jqXHR.done(),jqXHR.fail(),   和jqXHR.always()相反。