1

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

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

}

2
  • could you indent your code, its difficult to read like that! Commented Feb 15, 2010 at 12:54
  • Yeah, sorry... it's fine now :) Commented Feb 15, 2010 at 12:57

3 Answers 3

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:

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>

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;
        }
    });           
}
Sign up to request clarification or add additional context in comments.

Comments

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?

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

3 Comments

Hi James, Yeah, the URL has a real value in my code. I'm using other request in other places and they work perfect, but this one in particular is being called when a link is clicked so, I thought probably I was not allowed to do that...
Hi! I think the problem is the link itself. The event is triggered and there is no chance for the request to be made. Should I prevent the link AFTER the request? In the success function? or BEFORE? Thank you!
Are you still wanting the anchor link to function? If not then you can stop the event. See my answer to the following post: stackoverflow.com/questions/2118560/…
1

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

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

After 'success'

2 Comments

Hi! I think the problem is the link itself. The event is triggered and there is no chance for the request to be made. Should I prevent the link AFTER the request? In the success function? or BEFORE? Thank you!
Anything you want to happen afterwards, you can call from inside the success method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.