[Solved] How to call a C# static method from jQuery


For the first time in my years of programming, I’ve had to change an AJAX call to synchronous for it to work the way I wanted it. All that was needed was to add the “async: false” attribute to the AJAX call. This way the AJAX call is treated like any other synchronous function:

function Translate(word) {
    var Url = "/api/translate/" + word;
    var translated;

    $.ajax({
        type: "GET",
        url: Url,
        dataType: "json",
        cache: false,
        async: false,
        success: function (response, status, xhr) {
            translated = response.translatedWord;
        },
        error: function (jqXhr, textStatus, errorThrown) {
            var error = CapitalizeFirst(textStatus) + ": " + errorThrown + " - " + jqXhr.responseText;
            bootbox.alert(error);
        }
    });

    return translated;
}

For those of you thinking of implementing this as a solution for your project, THINK AGAIN! Making an AJAX call synchronous is no trivial matter. Doing so will force your users into waiting for a response back from the server before the program is responsive again. Unless you’re sure the data set is small and its access is not very frequent, you should look for other ways.

3

solved How to call a C# static method from jQuery