jQuery Ajax HTTP请求进入一个单击函数——不工作

时间:2022-10-07 15:39:33

My question is:

我的问题是:

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).

是否可以使用jQuery在单击函数中执行Ajax请求?(见下面的例子),如果是,我做错了什么?因为我不能执行任何请求(我通过我的success函数通知数据,并且没有检索到任何内容)。

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");                                        
    })                  
}

}

}

3 个解决方案

#1


1  

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:

如果锚与href属性链接,那么这可能会中断AJAX请求。最近在下面的堆栈溢出post中讨论了一个类似的问题:

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

如果您真的想要使用AJAX进行链接跟踪,您可能需要做以下工作:

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

With the following JavaScript logic:

使用以下JavaScript逻辑:

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

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

#2


1  

Have you considered the possiblity that the request might be failing. If so, you're never going to hit the alert.

你考虑过这个请求可能失败的可能性吗?如果是这样,你就永远不会进入警报。

Can you confirm that the beforeSend callback is being fired?

您能确认前面的回调正在被触发吗?

Also, I'm assuming 'myURL' isn't that in your real-world source code?

另外,我假设“myURL”在你的真实源代码中不是吗?

There may also be something awry in the }, that closes your function.

在}中也可能有错误的东西,关闭函数。

#3


1  

Im guessing some sort of error is being generated. Try adding

我猜正在生成某种错误。尝试添加

  error:function(a,b){
        alert(a);             
    },

After 'success'

“成功”后

#1


1  

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:

如果锚与href属性链接,那么这可能会中断AJAX请求。最近在下面的堆栈溢出post中讨论了一个类似的问题:

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

如果您真的想要使用AJAX进行链接跟踪,您可能需要做以下工作:

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

With the following JavaScript logic:

使用以下JavaScript逻辑:

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

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

#2


1  

Have you considered the possiblity that the request might be failing. If so, you're never going to hit the alert.

你考虑过这个请求可能失败的可能性吗?如果是这样,你就永远不会进入警报。

Can you confirm that the beforeSend callback is being fired?

您能确认前面的回调正在被触发吗?

Also, I'm assuming 'myURL' isn't that in your real-world source code?

另外,我假设“myURL”在你的真实源代码中不是吗?

There may also be something awry in the }, that closes your function.

在}中也可能有错误的东西,关闭函数。

#3


1  

Im guessing some sort of error is being generated. Try adding

我猜正在生成某种错误。尝试添加

  error:function(a,b){
        alert(a);             
    },

After 'success'

“成功”后