jQuery的Ajax的HTTP请求到一个点击功能 - 不工作

问题描述:

我的问题是:

是否有可能做一个Ajax请求中的点击功能,使用jQuery? (见下面的例子),如果是的话,我究竟做错了什么?因为我不能够做任何要求(我通过我的成功的功能并没有什么提醒检索数据)。

Is it possible to do an Ajax request WITHIN a click function, with jQuery? (see example below), If so, what am I doing wrong? Because I'm not being able to do any request (I'm alerting the data through my success function and nothing is being retrieved).

非常感谢您事先的任何帮助! :)

Thank you very much in advance for any help! :)

 function tracker(){

    this.saveEntry = function(elementTracked, elementTrackedType){      
    var mode = "save";
    var dataPost = "mode="+mode+"&elementTracked="+elementTracked+"&elementTrackedType="+elementTrackedType;        
    $.ajax({
        type: "POST",
        url: 'myURL',
        data:dataPost,
        success:function(msg){
            alert(msg);             
        },
        beforeSend:function(msg){
            $("#trackingStatistics").html("Loading...");
        }
    });
    return;
},
    this.stopLinksSaveAndContinue = function(){
    var fileName;
    $("a[rel^='presentation']").click(function(e){      
        fileName = $(this).attr("rel").substring(13);   
        this.saveEntry(fileName,"Presentation");                                        
    })                  
}

}

如果你的锚与的href 属性链接,那么这可能会打扰到你的AJAX请求。类似的问题,最近在下面的堆栈溢出后讨论:

If your anchor is linked with the href attribute, then this may be interrupting your AJAX request. A similar problem was recently discussed in the following Stack Overflow post:

  • window.location更改失败AJAX调用

如果你真的想坚持使用AJAX的链接跟踪,你可能要做到以下几点:

If you really want to stick to using AJAX for link tracking, you may want to do the following:

<a href="#" onclick="tracker('http://www.google.com/'); return false;">Link</a>

通过下面的JavaScript逻辑:

With the following JavaScript logic:

function tracker(url) {
    $.ajax({
        type: 'POST',
        url:  'tracker_service.php',
        data: 'some_argument=value',

        success: function(msg) {
            window.location = url;
        }
    });           
}