简体   繁体   English

成功进行ajax调用后,链接href

[英]Following link href after successful ajax call

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: 我已将点击绑定到事件以发布这样的ajax请求:

$('.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 我知道我可以将window.location.href放在成功处理程序中,但我试图尽可能避免这种情况

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). 不幸的是(在你的情况下),ajax是异步的,这意味着你的click函数启动ajax调用然后继续运行而不注意它的作用(因此最后不返回任何内容)。

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. 成功函数稍后调用(当ajax成功返回时)并且是一个完全不同的函数,因此它的返回true与您的原始单击函数无关。

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). 所有这些,你需要使用javascript来覆盖锚标签的自然行为(直接转到google.com)以及之后发生的事情(重定向)。

$('.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. 它非常动态,并且支持所有支持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. 如果我们需要在这里实际“点击”,我们应该只是新触发click事件并允许它在第二次点击时返回true。

$('.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();
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM