I want to make $.get() method synchronous in my function. In ajax aysnc : false
helps me, now how do i do the same to to $.get()
var ifsc_code = $('#ifsc-code').val();
var api_url = '/'+ifsc_code;
$.get(api_url, function(data, status){
//console.log(data.status);
var status = data.status;
if (data.status == "success") {
$.each(data,function(key,value){
var address = value.BANK+", " + value.BRANCH+", " + value.ADDRESS ;
$('.bank-details').removeClass('no-ifsc').text(address);
});
var res = "true";
alert('inside checkIfsc true');
return res;
}
else if (data.status == "failure") {
$('.bank-details').addClass('no-ifsc').text(data.message);
var res = "false";
alert('inside checkIfsc false');
return res;
}
Or is there any other approach to do the same ?
I want to make $.get() method synchronous in my function. In ajax aysnc : false
helps me, now how do i do the same to to $.get()
var ifsc_code = $('#ifsc-code').val();
var api_url = 'http://api.techm.co.in/api/v1/ifsc/'+ifsc_code;
$.get(api_url, function(data, status){
//console.log(data.status);
var status = data.status;
if (data.status == "success") {
$.each(data,function(key,value){
var address = value.BANK+", " + value.BRANCH+", " + value.ADDRESS ;
$('.bank-details').removeClass('no-ifsc').text(address);
});
var res = "true";
alert('inside checkIfsc true');
return res;
}
else if (data.status == "failure") {
$('.bank-details').addClass('no-ifsc').text(data.message);
var res = "false";
alert('inside checkIfsc false');
return res;
}
Or is there any other approach to do the same ?
Share Improve this question asked May 25, 2017 at 4:50 Naveen KumarNaveen Kumar 1,6166 gold badges30 silver badges54 bronze badges 1- 1 Why do you want to do this? It's almost always a very bad idea. If the server is slow to respond, it will lock up the browser. And in some browsers, it will lock up every browser window and tab. If the purpose is to make your code easier to write, you should bite the bullet and write proper asynchronous code that avoids these problems. – Michael Geary Commented May 25, 2017 at 5:03
3 Answers
Reset to default 12$.get also accepts parameter like this
$.get({
url: url,// mandatory
data: data,
success: success,
dataType: dataType,
async:false // to make it synchronous
});
Although already answered:
$.ajax({
method: 'GET',
async: false
});
I want to warn of you the following:
- Blocks javascript execution, and might thus lock the webpage (no buttons working etc etc, it looks like your page hangs during the ajax call)
- Using synchronous calls gives console warnings.
Javascript is build based on event-driven design. you could also fire an custom named event (with the data result in the event data) when the ajax/get call is complete and let the followup action listen to this event.
eg:
$(form).on('submit',function(){
var formEl = this;
$.get({...}).success(function(data){
var event = new Event('data-submitted');
event.data = data;
formEl.dispatchEvent(event);
})
});
$(form).on('data-submitted',function(event){
var data = event.data;
// you can handle the post ajax request event here.
});
add an object parameter
$.get({
url: 'your-url',
async:false
});