-1

I am unable to check if my code is wrong or there is any syntax error here. I spent more time so thought of asking in stackoverflow.

$(document).ready(function () {

    AjaxGet = function (url, storeageLocation, mySuccessCallback) {
        var result = $.ajax({
            type: "GET",
            url: "/Regions/GetView",
            async: true,
            contentType: 'application/json',
            dataType: "html",
            success: function (viewData) {
                alert(viewData);
                storeageLocation = viewData; 
                mySuccessCallback(viewData);
            },
            error: function (xhr, ajaxOptions, thrownError) {

            }
        }).responseText;

        return result;
    };
    alert(result);
});

3 Answers 3

1

The only problem here that I think is, that you cannot return a value from an Ajax call.

Try changing the return result and I think that would work. There isn't any trouble in your code.

Or just try changing the ajax request type to Synchronous. Maybe it would be like

async: false;
Sign up to request clarification or add additional context in comments.

Comments

1

Ajax is an async technology. So, you trying to return result before getting the response.

You can turn off async for ajax and that will look like so:

$(document).ready(function () {
    AjaxGet = function (url, storeageLocation, mySuccessCallback) {
        var result = null;

        $.ajax({
            type: "GET",
            url: "/Regions/GetView",
            async: false,
            contentType: 'application/json',
            dataType: "html",
            success: function (viewData, textStatus, jqXHR) {
                alert(viewData);
                storeageLocation = viewData; 

                result = textStatus;

                mySuccessCallback(viewData);
            },
            error: function (xhr, textStatus, thrownError) {
                result = textStatus;
            }
        });

        return result;
    };
    alert(result);
});

Comments

1
        $(document).ready(function () {

            AjaxGet = function (url, mySuccessCallback) {
                var result = $.ajax({
                    type: "GET",
                    url: "/Regions/GetView",
                    async: true,
                    contentType: 'application/json',
                    dataType: "html",
                    success: function (viewData) {
                        mySuccessCallback(viewData);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {

                    }
                }).responseText;

                return result;
            };
            // alert(result);  // will throw error: undefined variable 'result', because you used this variable inside function definition 'AjaxGet', not in this context
            AjaxGet( '/', function(data) { // call the function after defining it to execute and get the results
                alert( data );
            } );
        });

Comments

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.