最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Following link href after successful ajax call - Stack Overflow

programmeradmin0浏览0评论

I've got a normal link:

<a href="" class="continue">Continue</a>

I've bound the click to an event to post an ajax request like this:

$('.continue').click(function () {

    $.ajax({
        type: 'POST',
        url: '/url-to-post-to',
        data: mydata,
        contentType: 'application/json',
        error: function (err) {
            alert("error - " + err);
        },
        success: function () {
            return true;
        }
    });
});

What I'm trying to get to happen is the link to be followed on success....
ie, if there is no error, user should be redirected to google.

I know I can put window.location.href in the success handler, but I was trying to avoid this if possible

I've got a normal link:

<a href="http://www.google.com" class="continue">Continue</a>

I've bound the click to an event to post an ajax request like this:

$('.continue').click(function () {

    $.ajax({
        type: 'POST',
        url: '/url-to-post-to',
        data: mydata,
        contentType: 'application/json',
        error: function (err) {
            alert("error - " + err);
        },
        success: function () {
            return true;
        }
    });
});

What I'm trying to get to happen is the link to be followed on success....
ie, if there is no error, user should be redirected to google.

I know I can put window.location.href in the success handler, but I was trying to avoid this if possible

Share Improve this question asked May 15, 2013 at 11:02 AlexAlex 38.5k54 gold badges213 silver badges339 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 11

Unfortunately (in your case), ajax is asynchronous, meaning that your click function starts the ajax call and then continues running without paying any attention to what it does (and thus returns nothing at the end).

The success function is called later (when the ajax returns successfully) and is a completely different function, so its returning true has no bearing on your original click function.

All this to say, you will need to use javascript to override the anchor tag's natural behavior (go directly to google.com) and what happens afterward (redirect).

$('.continue').click(function (e) {
    e.preventDefault(); // otherwise, it won't wait for the ajax response
    $link = $(this); // because '$(this)' will be out of scope in your callback

    $.ajax({
        type: 'POST',
        url: '/url-to-post-to',
        data: mydata,
        contentType: 'application/json',
        error: function (err) {
            alert("error - " + err);
        },
        success: function () {
            window.location.href = $link.attr('href');
        }
    });
});

Have you tried something like this:

$('.continue').click(function(e) {
    e.preventDefault();
    var link = $(this).attr('href');

    $.ajax({
        type: 'POST',
        url: '/url-to-post-to',
        data: mydata,
        contentType: 'application/json',
        error: function (err) {
            alert("error - " + err);
        },
        success: function () {
            window.location.href = link; // <-- This line
        }
    });
});

Or if the link is send with the responding text, use that data.

Edit: I didn't saw your last line, what's the reason for not wanting to use that? It's perfectly dynamic and supported by all browsers that support JavaScript.

You can try this,

     $('.continue').click(function () {
   $.ajax({
    type: 'POST',
    url: '/url-to-post-to',
    data: mydata,
    contentType: 'application/json',
    error: function (err) {
        alert("error - " + err);
    },
    success: function () {

       $(location).attr('href',$(this).attr('href'));
    }
  });
  return false;
 });

If we need to actually have a "click" here, we should just trigger the click event newly and allow it to return true on the second click.

$('.continue').click(function () 
{
    if ($(this).attr('tracked')!=undefined)
    {
        return true;
    }
    a = $(this);
    $.ajax({
        type: 'POST',
        url: '/url-to-post-to',
        data: mydata,
        contentType: 'application/json',
        error: function (err) {
            alert("error - " + err);
        },
        success: function () {
            $(this).attr('tracked',true);
            $(a)[0].click();
        }
    });
});
发布评论

评论列表(0)

  1. 暂无评论