0

I'm running into a problem with a function call. What appens is I'll call Test() on a click event, and inside Test() there is a call to UpdateType(). For some reason though UpdateType runs twice. The first time it'll run and skip the ajax call altogether (and return "undefined"), then immediately run again and execute the ajax call. On the second time though, it does not finish the "if(UpdateType(1))" statement. If I click it a second time, it will alert "Huzzah!".

HTML:

<h1 onclick="Test();">Package Designer</h1>

Javascript:

        function UpdateType(Type)
        {
            var returnValue;
            if (intCurrentType != Type)
            {
                $.ajax({
                    type: "POST",
                    url: "PackageDesigner.aspx/SetPackageType",
                    data: "{'strType' : '" + Type + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: "true",
                    cache: "false",
                    success: function (msg)
                    {
                        alert(msg.d);//  **alerts "1"**
                        switch (msg.d)
                        {
                            case "-5": //Invalid ID
                                alert('You have been idle for too long');
                                window.location.replace("PackagesManager.aspx");
                                returnValue = false;
                                break;
                            case "-4": //invalid
                                alert('Not a Type.');
                                returnValue = false;
                                break;
                            case "-3": //Server Error
                                alert('There was an error with your request.');
                                returnValue = false;
                                break;
                            case "-2": //Not Authenticated
                                window.location.replace("Login.aspx?ReturnUrl=PackageDesigner.aspx");
                                returnValue = false;
                                break;
                            case "-1": //General Error
                                alert('There was an error with your request. Please try again.');
                                returnValue = false;
                                break;
                            default: //Success
                                intCurrentType = Type;
                                returnValue = true;
                                break;
                        }
                    },
                    Error: function (result)
                    {
                        alert('error');
                    }
                });
                return returnValue;
            }
            else
            {
                return true;
            }
        }
        function Test()
        {
            if(UpdateType(1))
            {
                alert("Huzzah!!");
            }
        }

I tried isolating the code in a blank webpage, and it still does the same thing, so I feel like I ruled out the issue of it being something else on the page.

Any help is appreciated.

Vandel212
  • 1,074
  • 1
  • 13
  • 28
  • 2
    Probably a dup of [How to return the response from an Ajax call](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call), Also, the `async` and `cache` arguments to `$.ajax()` are supposed to be Boolean, not strings. – jfriend00 Jul 27 '15 at 03:22
  • Spent some time thinking about it, and I realized I need to encapulate some code on the server side to make this work. Thank you. – Vandel212 Jul 27 '15 at 05:19

0 Answers0