JQuery - 获取请求后重定向

时间:2016-01-13 18:09:41

标签: jquery ajax get

是否可以在执行请求时重定向用户?我有这段代码:

$('.createNewPost').on('click', function(e) {

    e.preventDefault();

    var notLoggedIn = false;

    $.get($(this).attr('href'), function(data) {

        if (data.error === 1) {
            $('.signIn').trigger('click');
            return false;
        }

        // If user is logged in, transfer him to the create new topic page
        // window.location.replace($(this).attr('href'));
        window.location.href = $(this).attr('href');

    }, 'json');

})

我正在获取我想要的页面,但是通过ajax。我需要将用户重定向到下一页(两个被调用的方法都是一样的。)

1 个答案:

答案 0 :(得分:0)

this不是你认为它在回调中的内容,它有一个新的上下文

尝试将值存储在回调之外:

 $('.createNewPost').on('click', function(e) {

     e.preventDefault();

     var notLoggedIn = false;

     var href = $(this).attr('href');

     $.get(href, function(data) {

         if (data.error === 1) {
             $('.signIn').trigger('click');
             return false;
         }

         // If user is logged in, transfer him to the create new topic page
         // window.location.replace($(this).attr('href'));
         window.location.href = href;

     }, 'json');
 });
相关问题