yoozer8
yoozer8

Reputation: 7489

AJAX does not synchronously

I've got an issue similar to this question here: Javascript and AJAX, only works when using alert()

I have an AJAX call to update a field in the database when finalizing a form. However, the call doesn't seem to be reaching the controller. When I put an alert in, it works. I've currently got it set to synchronous because of the issue, but even then, without the alert, it only reaches the controller about 1 in every 5 or six times. This is the AJAX bit:

function finalize() {
    if (!confirm("Are you sure?"))
        return;

    $("#finalizebutton").attr('disabled', 'disabled');

    var request = new XMLHttpRequest();
    var url = "/Operation/Finalize/";
    request.open("GET", url, false);
    request.send();

    $("#otherformstuff").attr('disabled', 'disabled'); //a few disable calls
}

On the control side I have approximately:

public ActionResult Finalize()
{
    db.setfinalized(true);   //breakpoint here that only gets hit about 1 in 5 tests
    return Content("Finalized");
}

Even when there is an alert shoved in there it sometimes doesn't work. Oddly enough, I put an alert for request.responseText and it gave me the response from the control without the control ever actually processing it... Any thoughts on this are appreciated.

Upvotes: 0

Views: 583

Answers (1)

McKayla
McKayla

Reputation: 6959

Because you tell it to be synchronous.

Your problem is with this line:

request.open("GET", url, false);

The open method takes 3 arguments. Method, url, and the third is a boolean that tells it whether or not the request should be asynchronous.

Just change the false to true, and the request will be async.

request.open("GET", url, true);

But you could also just replace that entire block of XHR code with this one line..

$.get('/Operation/Finalize/');

Upvotes: 1

Related Questions