简体   繁体   中英

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. Firebug net status shows that no calls were made.strange

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

Try .done,.fail and .always

The success and error callbacks were deprecated in jquery 1.8

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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