简体   繁体   English

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

[英]ajax success error function doesn't call up in jquery

Hi here is my code.. 嗨,这是我的代码。

<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>

i am trying to access a.txt which is in the same directory.But the success and error function never calls up which is not understandable. 我正在尝试访问同一目录中的a.txt。但是成功和错误功能永远不会调用,这是无法理解的。 Firebug net status shows that no calls were made.strange Firebug网络状态显示未进行任何呼叫。

however if i use 但是如果我用

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

it does work. 它确实有效。

Need to add this to submit handler 需要添加此内容以提交处理程序

event.preventDefault()

Also changing POST to GET works for me on IIS 在IIS上也将POST更改为GET

Try .done,.fail and .always 尝试.done,.fail和.always

The success and error callbacks were deprecated in jquery 1.8 在jQuery 1.8中不建议使用成功和错误回调

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. 弃用通知:从jQuery 1.8开始,不再使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。 To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead. 要准备将其最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM