weixin_33692284 2016-10-16 06:18 采纳率: 0%
浏览 24

如果声明后Ajax调用

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.

  • 写回答

2条回答 默认 最新

  • weixin_33725270 2016-10-16 06:31
    关注

    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.

    评论

报告相同问题?