user1128677 user1128677 - 2 months ago 7
jQuery Question

jquery: make get() before form submitting

here is my code. Question is how can i make async get() query before form submit? One another callback? How should it looks like?

$('#auth').submit(function(e) {
var self=this;
e.preventDefault();
$.get(url, function(data) {
//some code
});
self.submit();
return false;
});

Answer

Submit the form only when the request has finished

$('#auth').submit(function(e) {
    e.preventDefault();

    var self = this;

    $.get(url, function(data) {
        //some code
        self.submit();
    });
});

It generally makes little sense to do an ajax request, sending data to the server, right before you submit a form that also sends data to the server.

Comments