0

I'm having a trouble since jQuery.ajax, in this particular case, doesn't seem to happen, so that the function always return NaN undefined as result:

    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)
            {
                return search_return["keywords"];
            }
        });
        delete cookie;
    }

I've also tried to write something like

success: function() { return "<nothing happens>"; }

But what I receive is undefined.

Please answer, I'm really freaking out with that.
Thanks in advance.

6
  • can you explain in more detail what you are trying to do? Commented Nov 19, 2011 at 14:30
  • Using "delete" on that local "cookie" variable is not really necessary. Commented Nov 19, 2011 at 14:31
  • Ok. thank you, Pointy. I'm new to jQuery and sort of to Javascript, so I didn't get that used. I forgot it has "garbage collection". Commented Nov 19, 2011 at 18:15
  • Yes. I can, Ashok Padmanabhan. What I'm trying to do is a search page that sends and receives data from the server. The point here is to know what was typed by the user when the search page appears, or other search keywords are sent to the server by the user. It works a little like this: in every page, there is a search "box" formed by a fieldset > table > tbody > tr > td-content editable, td-button order where the click on the button td redirects the user to the search page and makes the server generate a code. Commented Nov 19, 2011 at 18:19
  • Sorry I didn't answer immediately, but I was really busy. Commented Nov 19, 2011 at 18:41

2 Answers 2

2

What you're trying to do is fundamentally impossible. Your ajax operation is asynchronous (no it isn't durrr).

Instead, re-architect your API:

function requestUploadedSearch( callback )
{
    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)
        {
            callback( search_return["keywords"] );
        }
    });
    delete cookie;
}

Then, when you invoke it, instead of expecting a return value, pass in a function to respond to the returned data:

requestUploadedSearch( function( value ) {
  // ... do stuff with "value" ...
});

editDoh! @nickd is correct; since you're making the call synchronously (which you really should seriously consider not doing; it's pretty bad for your user experience) the story is different. Still, however, the approach above would work.

Sign up to request clarification or add additional context in comments.

4 Comments

OP has async: false, so the call blocks, no?
Well... but that is a point. My function makes synchronously the request. So, I think It should call success method.
@nickd oops and I saw that too! My brain just flipped it over. I blame lack of coffee :-) Thanks, I'll update the answer.
Sorry user1055350 you're right, but that's what nickd's comment meant also :-)
1

Pointy is pointing (hah) you in the more usual way of doing things. However as you have async: false set, your success: function is never called. Putting

return search_return["keywords"];

after the delete cookie line will return as you have it in your example, but you aren't using the result of the ajax call anywhere I can see, so I'm not sure that there's any point.

2 Comments

But how? "search_return" is the result of the ajax operation. it is declared as var search_return={"keywords" : "something", "code" : "<<54 character code>>"}
code is like the server's "address" to your search keywords, so they are easily managed by the administrator. It is saved as a string pair in the server (was wrote in delphi) formed by <<the code string>>=<<the keywords string>>.

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.