1
function IsSwap()
    {
        var urlString = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/GetModelType")%>";
        var id = 
            {
                id : GetGUIDValue()
            }
            $.ajax({
                type: "POST",
                url: urlString,
                data: id,
                success: function(data) {
                    if (data.toString() == 'SwapModel')
                    {
                        return true;
                    }
                }
            });

Expected result is true. I can alert right before the return so I know it's getting to that point fine. In another function, I tried to get my bool and use it like this:

var isSwap = IsSwap();
            if (isSwap)

and it keeps saying isSwap is undefined. Why?

7
  • IS isSwap defined? Are you calling the function before it has been initialized? Commented Feb 28, 2011 at 16:04
  • at a first glance, you need to escape those double quotes in the urlString variable Commented Feb 28, 2011 at 16:05
  • the function or the variable? the function is being called fine yes. I can place an alert right before the return statement and see it popup when it needs to. Something is changing "true" to undefined on the call though when I assign it to the variable isSwap. Commented Feb 28, 2011 at 16:05
  • @Kyle -- it calls fine though. Goes through the controller, back onto the client side, and sets the return to true. For some reason the variable is being assigned true in the call to the function though. Commented Feb 28, 2011 at 16:06
  • 4
    IsSwap does not return a value, so the function returns undefined. The return true is from the Ajax success callback, not IsSwap. Commented Feb 28, 2011 at 16:07

4 Answers 4

3

You are using ajax requests, which are asynchronous. You can't return value from an ajax request. Try this instead:

function IsSwap()
    {
        var urlString = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/GetModelType")%>";
        var id = 
            {
                id : GetGUIDValue()
            }
            $.ajax({
                type: "POST",
                url: urlString,
                data: id,
                success: function(data) {
                    if (data.toString() == 'SwapModel')
                    {
                        ResultIsTrue(); // call another function.
                    }
                }
            });

After execution, instead of using this:

var isSwap = IsSwap();
if (isSwap){
 // Do Somethinh
}

Try that:

IsSwap();
function ResultIsTrue(){
 // Do Same Thing
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can't return from an ajax call like that.

Essentially what you're doing is returning true from the inner function.

You should just call whatever code you need in the success method as that's the only time you can guarantee that the ajax call has completed.

3 Comments

You still couldnt return it directly, but you could create a local variable inside the IsSwap function, set that on success and then return that.
@slandau no, it won't. IsSwap does not return a value, so you get an undefined return value by default.
You should put the required logic within the success: function(data) fn. If that isn't possible you could pass a calback function to IsSwap which gets called on success.
0

The AJAX request is asynchronous, meaning that IsSwap will return before the response to the AJAX request.

Whatever you need to do based on isSwap, you should do in the success handler of your AJAX request.

UPDATE: This last paragraph is incorrect about it working, but worth noting about synchronous not being recommended:

Alternatively, you could make the AJAX request synchronous (by adding async:false to the AJAX options) and keep it how you have it - however I wouldn't recommend this as your browser will 'freeze' whilst it waits for the response.

1 Comment

It doesn't matter whether it's synchronous, he's not returning a value from IsSwap.
-2

Check this line:

var urlString = "<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/GetModelType")%>";

Try using single quotes like so:

var urlString = '<%= System.Web.VirtualPathUtility.ToAbsolute("~/mvc/Indications.cfc/GetModelType")%>';

Your double quotes inside double quotes is most likely your problem.

2 Comments

It makes the controller call absolutely fine though and gets to the last return statement.
-1 The string is fine. The problem is that an Ajax call is async so the logic needs to be in the success handler function.

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.