views:

143

answers:

1

I'm trying to save some content whenever a button/hyperlink is clicked using jquery.ajax (Using Asp.net 3.5). The logic is as follows:

  • Through .bind in jquery I bind my own method(MakeLog) to a button click or hyperlink click. The click events of button/hyperlink contain nothing, I need to use .bind for selective controls.
  • Now we have a button whose click event will fire a method, say MakeLog.
  • Code snippet for MakeLog is as follows:

.

var xhr = jQuery.ajax({  
    url: "/Logger.aspx",  
    data: { content: logContent },  
    error: function(XmlHttpRequest, textStatus, errorThrown) {
        alert ("XmlHttpRequest: " +   XmlHttpRequest + 
               " textStatus: " + textStatus + "errorThrown: " + 
               errorThrown);
    },
    contentType: "Content-Type: text/html; charset=utf-8",  
    success: function (xml, txtStatus, XmlHttpRequest) {  
       //alert("xml" + XmlHttpRequest);  
    },  
    async: true  
});  
A: 

This sounds like a similar issue I encountered with AJAX event logging. This can be solved by loading pixel images via JavaScript. The reason is that page loads interrupt XmlHttp requests (your AJAX calls). Loading images, however, will not be interrupted by page loads.

See the following question I posted previously for reference: http://stackoverflow.com/questions/3214023/ajax-get-race-condition

Matt Huggins
Matt, Should I be putting that image request in my method before the ajax call? And wouldn't this be a synchronous call? Because I don't want synchronous communication in this method.
Sidharth Panwar
It should *replace* the AJAX call. Take a look at the Image object in JavaScript: https://developer.mozilla.org/en/Canvas_tutorial/Using_images#section_4
Matt Huggins
I tried the following, but to no avail: 1. Added an empty image 2. Modified my function as follows: var image = new Image(1, 1); alert(image + "image"); image.onload = function () { <ajax call here> } img.src = "<empty image file src>"; Is this what you meant?
Sidharth Panwar
Also tried doing this: image.src = "/Logger.aspx?content=asdf"; (Setting image source to asp.net page and passing data through querystrings, again this works if there's an alert but not otherwise.)
Sidharth Panwar
No, you don't need an AJAX call. The image you're loading replaces that. Try something like this: `var img = new Image(1,1); img.src = '/Logger.aspx?any=data` ... your Logger.aspx file should perform the tracking as it normally does based upon the parameters, but return a 1x1 pixel image or something. The uniqueTimestampHere value should just be the Javascript milliseconds since the Epoch or something to prevent the pixel image from being cached across requests.
Matt Huggins
Tried this but isn't working. I think the page reload/redirect might be cancelling the call, because the call is being made (I checked in firebug).
Sidharth Panwar