tags:

views:

242

answers:

8

I'm a total noob when it comes to jQuery but I am trying to learn. What in tarnation am I doing wrong here?

function MakeCall(url) { 
var result;
$.ajax({
    url: url,
    dataType: "text",
    success: function(txt) {
        result = txt;
        return;
    }
});
alert(result);
return result;
}

EDIT:

ok, sorry about that bit of stupidity... i've fixed that part. now my problem is that the alert where it is now is returning "undefined", even though the result is getting set properly in the success function...

Thanks so much...

+13  A: 
    result == txt;

Should be

    result = txt;


Your second problem is that ajax calls are asynchronous. i.e. the alert(result) statement will probably be executed before the success callback function.

You are asking how to prevent this. You can't. It's not a bug. This is what AJAX stands for. If you want something to be executed after the ajax call, add it to the success callback function.

Edit

If you really need the result to be processed in some other function then you can do this:

$.ajax({
    url: url,
    dataType: "text",
    success: function(result) {
        someOtherFunction(result)
        return;
    }
});
Nadia Alramli
so there's no way to return a result from an ajax call? somehow i find that hard to believe....
Jason
+2  A: 

The assignment operator in javascript is =

result = txt;
Vincent Ramdhanie
+2  A: 

The second returns undefined because nothing is ever assigned to result. As the others say,

change it to

 result= txt;

result==txt is a way to compare, not to assign

Edit-

The second problem is because the javascript will not wait for the ajax to finish before it does its alert . The A in ajax stands for Asynchronous meaning other things will run not wait for the ajax function to finish

TStamper
+2  A: 

This is happening because the alert is happening immediately after the AJAX is dispatched. AJAX is Asynchronous, so simply dispatching the request is not sufficient to populate the result.

Put the message box inside your success function, or call a function from within your success function.

John Gietzen
+3  A: 

I think it's due to the asynchronous nature of javascript server calls: javascript doesn't wait for the response before proceding to the alert() call

TenebrousX
+2  A: 

Perhaps the ajax call has yet to return when you do the alert?

Try putting an alert in the success function to test if it is returning at all.

Tom Hubbard
+3  A: 

Apologies - I'm cross posting an answer to the question, only saw the other question this guy posted originally.

Jquery's ajax functions are asynchronous - therefore the success function will be called after the second alert is run. It will then return from the function before the ajax method is completed.

If you really want to build the app this way, you can add an async option to the call, setting it to false. (http://docs.jquery.com/Ajax/jQuery.ajax#options, see the first entry.) This will result in the success function being called before the $.ajax method returns.

However, doing blocking ajax calls is not recommended, as it will hang the script and the browser.

Therefore, I advise you to restructure the application as such:

function MakeCall(url, callback) { 

$.ajax({
    url: url,
    dataType: "text",
    success: callback
});

}

MakeCall('http://theurl.com', function(txt) {
        result = txt;
        alert(result);
        return;
});
Kazar
+1 I like your solution
Nadia Alramli
+1  A: 

The $.ajax() function is asynchronous by default (it's the A in AJAX) so the alert happens before the AJAX callback function is complete. If you need your function to return the value, use the async option.

$.ajax({
    async: false, // disable asynchronous ajax
    url: url,
    dataType: "text",
    success: function(txt) {
        result = txt;
        return;
    }
});

That will freeze the browser until the function is complete, so if you can, it is better to let it run asynchronously and use the result in the success callback function as mentioned by others.

mcrumley