Industrial
Industrial

Reputation: 42758

jQuery: Making synchronous requests properly

Ok, so I need to open a dialog (jQuery UI) before performing the actual AJAX request and depending on what the user chooses, continue with the AJAX request or simply do nothing.

Unfortunately the function seems to be returning early and ignores the synchronous request completely. What am I doing wrong?

Here's the code:

function saveSomeStuff(value) {
    var returnvalue = this.revert;

    $("#dialog").dialog({
        draggable: false,
        buttons: {
            "OK": function() {
                $(this).dialog("close");
                returnvalue = $.ajax({
                    type: "POST",
                    url: config.someUrl,
                    async: false,
                    data : {/* some data */}
                }).responseText;
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    }); 

    return returnvalue;

 }

Upvotes: 2

Views: 1261

Answers (1)

griZZZly8
griZZZly8

Reputation: 694

$.ajax({
        type: "POST",
        url: config.someUrl,
        async: true,
        data : {/* some data */},
        success : function (data) { *YOUR LOGIC HERE*}
})

P.S. Avoid synchronous requests everywhere.

Upvotes: 2

Related Questions