I think your design is poor. The problem is you shouldn't be trying to "inject" an ajax call in the beforeSubmit callback. I suggest one of two things: at the point of making a request to the server, why not just do the validation step on the server side? Calling out to the server to find out if you should call out to the server seems poor. You should still be validating the data from the second ajax call anyways, since it would be trivial for a malicious user to bypass your first validation check.
If you *really* feel like 2 ajax calls is the way to go, make the first request be to validate, and make the second call in the callback from the first. --Erik On 3/31/07, tawm <[EMAIL PROTECTED]> wrote:
Hello people, I'm in a situation right now in which I do an AJAX call, supplying it a callback function (for success). In this callback function I do some operations which determine what happens after the AJAX call. Normally you would solve this by calling yet another callback function at the end of the success-callback. But now this is not possible because the function that did the original AJAX call is called from inside a plugin and should return a certain value (true or false) to instruct the plugin's consequent behaviour. The plugin I'm talking about is the jQuery form plugin, so the situation is as follows: --- begin example code --- // Activate the form plugin to enable AJAX submission of a form. $("#aForm").ajaxForm({beforeSubmit: validateForm}); // validateForm is called before the form is actually submitted. // It should return true if the form should be submitted after all (f.e. because all data is valid), // false in case it should stop (f.e. there is some invalid data in the form). function validateForm() { $.ajax({url: '/validateSomething.php', data: 'something', success: function(result) { // Here our validation call is done, so here we know whether we want validateForm() to return // true or false. Problem is: we are in a callback function and we cannot reach the function // from which we originate (validateForm). Any value that we return here is simply lost in // jQuery's $.ajax() function. if (result == 'validation succeeded') { return true; // This doesn't work because the return value is dropped by $.ajax(). } return false; // This doesn't work because the return value is dropped by $.ajax(). }); return; // Here we want to return true or false, depending on the behaviour that we expect from // the form plugin after this callback finishes. Problem is, here we don't know this value, // since it is determined in $.ajax()'s success callback function. } --- end example code --- Since I'm not very experienced with asynchronous stuff in Javascript I don't know whether this is a flaw in jQuery or an inevitable result of the way AJAX works. In the latter case, I guess that the architecture of the jQuery form plugin is flawed. Or I must be overlooking a possibility to return a value based on the outcome of a AJAX call done in the same function. I hope all is a bit clear. Thanks in advance. Tim Molendijk