0

I'm having serious problems with the function below:

    function requestUploadedSearch()
    {
        var cookie = JSON.parse(readCookie("user_search_cookie"));
        $.ajax({
            dataType: "script",
            async: false,
            data: {
                context: "search-get",
                code: removeNull(cookie, cookie !== null, "code")
            },
            success: function(data)
            {
                var keywords = search_return["keywords"];
                return keywords; // here is the damn problem.
            }
        });
    }

Seams that simply nothing comes out of the function except for the undefined value and no errors are shown in the debugger. And I'm seriously almost throwing my laptop on the wall. If anyone could help me doing this, please answer! Thanks in advance.

6
  • 1
    What problems, serious or otherwise, are you having? Have you used the JavaScript console to identify the problems, are there any errors reported? Commented Nov 19, 2011 at 20:07
  • What is this code try to accomplish. What is NOT working for you? Commented Nov 19, 2011 at 20:07
  • possible duplicate of Can't get correct return value from an jQuery Ajax call. The duplicate is about asynchronous calls, but the fundamental problem is the same: you're returning from the anonymous function, not from the main function. Commented Nov 19, 2011 at 20:08
  • 1
    We need to know what problem you are having. For instance, in your success function, I see you are using the value search_return, but you pass data into the function and don't use it. Commented Nov 19, 2011 at 20:08
  • Well, sorry for that, I promise I will edit it. The problem is: Nothing comes out of that damn function and nothing is reported! A undefined just appears at the end. Commented Nov 19, 2011 at 20:10

4 Answers 4

2

1st off: Where is the search_return variable? Why are you ignoring data?

I have a feeling this is what you want to do:

function requestUploadedSearch()
{
    var cookie = JSON.parse(readCookie("user_search_cookie"));
    var keywords;
    $.ajax({
        dataType: "json",
        async: false,
        data: {
            context: "search-get",
            code: removeNull(cookie, cookie !== null, "code")
        },
        success: function(data)
        {
            keywords = data["keywords"];
        }
    });
    return keywords;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Exactly! But how do I write the JSON in the server-side aplication?
@Hogan: Slightly different. You're using dataType: 'script', I'm using dataType: 'json' which drastically changes how the data being passed into the callback is interpreted.
@user1055350: That's a completely separate issue, and should probably be posted as another question.
I guess you could say it is different, however the OP clearly did not understand closures, which was main issue and at least explained in my answer.
1

The issue is that since the Ajax call will complete at an arbitrary time in the future, you cannot simply return a value from its success handler.

One issue is that you're not actually doing anything with the data returned by the server, which seems puzzling.

The nutshell version is that you need to implement the functionality as part of the success callback. That can be done in-line, or you can create the callback function outside of the Ajax call itself and use it as the value for the success property:

function onSuccess(data) {
    // Do something with the data here
}

...

$.ajax({ // etc.
    success: onSuccess
};

You may also use jQuery's $.when function.

3 Comments

He has set async: true though.
@flesk I think you mean false. But that doesn't mean returning a value from the success handler returns a value from the enclosing function. I'm also guessing the OP did that in attempt to do what he thought he was doing, rather than because it needs to be, or should be, synchronous.
Yeah, that's what I meant. :$ I agree with you on that, as I also posted in my answer. And yeah, that's probably the reason.
1

The problem is the scope you're trying to return your keywords from. The success function is called by jQuery, and you don't have any control over what jQuery does with that return value. You could do return $.ajax(... but you wouldn't get what you expect, since according to the documentation: "As of jQuery 1.5, the $.ajax() method returns the jqXHR object, which is a superset of the XMLHTTPRequest object" (http://api.jquery.com/Types/#jqXHR).

What you should do instead is set up a callback function like:

function doSomethingWithKeywords(keywords) {
    // do stuff
};

and in the success function call that function:

doSomethingWithKeywords(keywords);

EDIT: Hogan's is a good solution, since your call isn't asynchronous.

Comments

0

The problem you are having is the return you are passing is not the return of the function -- it is the return of the event of success. Often closures (implied passing of a local variable to a function) are used to solve this problem in JavaScript.

NB I still don't think your function will work because I don't see where search_return["keywords"] is defined. But at least you won't have to worry about the closure issue. Once your success function is correct the main function will return it.

Like this:

function requestUploadedSearch()
{
    var cookie = JSON.parse(readCookie("user_search_cookie"));
    var returnClosure;
    $.ajax({
        dataType: "script",
        async: false,
        data: {
            context: "search-get",
            code: removeNull(cookie, cookie !== null, "code")
        },
        success: function(data)
        {
            // returnClosure = data["keywords"];
            returnClosure = search_return["keywords"];
        }
    });
    return returnClosure;
}

1 Comment

search_return["keywords"] looks like it should be data["keywords"]

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.