we know that only synchrnous requests are done with $.ajax with an option of async:false
But i have seen in the Jquery manual saying that “synchronous requests may temporarily block the browser
disabling any actions while the request is active”. I have some questions such as
I have seen some answers saying to use callback () How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?.
How can i implement this to my function .
In the below Function if the Request Fails or if the response doesnot return How can i handle it .I mean Can i use “error:” or some thing like that .
How can I improve this Function efficently i mean using call back and error handling .It is said that (we can never have error and success call back with a request)
function empvalidate() {
var exists = false; // default return value is false
var empno = $("#empno").val();
if (empno != '') {
$.ajax({
url: "emp.php",
async: false,
dataType: "json",
data: {'param1': $("#param1").val(), 'empno': $("#empno").val()},
success: function (data) {
exists = data.status; // set status of existence to outer variable
}
});
}
return exists; // return actual value
}
Your synchronized request will cause the UI thread (and therefore the browser) to wait until the
XMLHttpRequestobject returns (kind regardless whether it failed or succeded).jQuery will execute your success / complete and error handler the same way it would execute when you’re using a “normal” asynchronous request. But it will wait / block before it does so.
So the worst case scenario here would be, that the actual request takes a huge amount of time. Network latencys are pretty bad in this context, large data amounts.. all those will entirely lock up your UI thread which is obviously an aweful user expierence.
I really don’t know where someone should use a synchronized ajax request nowadays.