I've tried async: false
but nothing seems to work for me.
var user = $('#element').text(),
// user == 'KendrisX'
ref;
$.ajax({
type : 'get',
async : 'false',
url : '/memberlist?username=' + user,
success : function(data) {
if ( $('#link', data).html() ) ref = $('#link', data).attr('href');
// ref == '/u1'
}
});
if ( ref != undefined ) {
// Code Block here
}
All I'm trying to do is to run the AJAX call and check if the variable ref
has been defined yet. If it has, then I want to run a certain code block.
All of this will be executed within jQuery's $.each
function so I plan to use $(this)
within the if statement (which won't work in the AJAX call.
Using async: false will not work in jQuery 1.8 or lower version. you can use the complete, success, error callbacks.
Another option is, just move your if condition into success method.
async : 'false',
'false'
is a string, which is a true value, it is not the same as false
which is a boolean false.
That said, synchronous requests on the main thread are deprecated, so don't do that. Handle the result in the success function instead.
All of this will be executed within jQuery's $.each function so I plan to use $(this) within the if statement (which won't work in the AJAX call.
Yes, it will. You just need to pass the right value in. (e.g. with bind
or an arrow function)
success : function(data) {
if ( $('#link', data).html() ) ref = $('#link', data).attr('href');
// ref == '/u1'
}.bind(this)