0

maybe it can't be done or maybe I just can't find it, either way I'm new to jquery and it's driving me nuts. I want to accept the return from an ajax call in the calling function, preferably a JSON object. I am explicitly looking for a way to handle the response outside of success/complete callback functions. Can someone please give me the proper syntax for this? The pseudo code is something like

SomeFunction()
{
    var JSON_Containter = .ajax({stuff...});

    DO other stuff with JSON response...
}

Thanks very much for your assistance!

4
  • You know AJAX is async right? Commented Mar 5, 2012 at 23:53
  • possible duplicate of Return value from ajax call? Commented Mar 5, 2012 at 23:55
  • This might work for you. Commented Mar 6, 2012 at 0:00
  • yes, I know AJAX is async. Asynchronous JavaScript And XML... I'm trying to work with Jeditable, submit to function instead of url. I looked at the code, Jeditable seems to run with async to false by default. Jeditable passes my function the submit value and expects a return of the same. I want to be able to alter the value and return it post-server side validation, but have been having a bugger of a time of it. Even when my ajax call is set to sync mode, the Jeditable text area just freezes up. I'm sorry if this is a dumb question, I'm new to client side scripting. Commented Mar 6, 2012 at 21:12

2 Answers 2

1

The only way to not use callback functions would be to make the AJAX request synchronous so that the code waits for it to complete:

var responseObj;

$.ajax({ 
    ..., 
    async: false, 
    success: function(data) { responseObj = data; }});

// Do stuff with responseObj

I wouldn't recommend doing things this way. You don't want the UI freezing while waiting for the AJAX response. Here's a better approach:

function SomeFunction(successCallback, errorCallback) {
    $.ajax({
        ..., 
        success: successCallback,
        error: errorCallback
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know why this was downvoted. works fine. I went with the former (see above comment). Thanks
i tried using JSON as well, and its really driving me nuts. its so dissapointing. i see a lot of sample codes but i just dont get it! its so frustrating!
0

You can use jQuery.getJSON(), but you are still going to need to use a success callback, because of the way AJAX works - the A stands for "asynchronous", after all.

But don't forget that inside that success callback, you have access to the variables from the parent scope as well, so you should be able to do what you need within that callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.