Martyn
Martyn

Reputation: 11

IE not returning results from jQuery ajax() call

First off I must apologise, I know questions similar to what I'm about to ask have been asked before, but none of the answers or suggestions given in those questions are working for me.

Heres the story:

I have written a function that sends an ajax request to the server which in turn runs a php script which performs a query on a database and returns an html element, specifically it returns: <a href="mypage.php">Click here</a>. This is my code:

$('#txtHint').hide();

    function showReviews(str) {
        $.ajax({
            xhr: function() {
                 if ($.browser.msie)
                 {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                 }
                 else
                 {
                     return new XMLHttpRequest();
                 }
            },
            url: "dropsearch.php",
            cache: false,
            type: "post",
            data: "q=" + str,
            dataType: "html",

    success: function(data) {
      $('#txtHint').append(data);
      $('#txtHint').show();
      window.alert(data);
    }
  });
}

Now, this works perfectly fine in normal browsers, the a tag is returned in the response header and appended to the txtHint div. However, with Internet Explorer, the response header isn't returning anything. I've even used firebug and fiddler to see exactly what the headers are sending and returning, and for some reason although IE is sending the correct header and receiving one back, it's not receiving the content. The window.alert() call just displays an empty alert box in IE.

I've been reading that IE has problems with cache when it comes to ajax calls; however, ensuring the request isn't cahced doesn't work.

Does any body have any idea why this is happening? It's been driving me absolutely nuts!

Any advice would be much appreciated.

Cheers

Upvotes: 1

Views: 3570

Answers (2)

Jonathan Persson
Jonathan Persson

Reputation: 451

It might be a bit late for an answer. But I myself have been struggling with the same problem for the last 5 hours.

In my case the problem was related to null characters being included in the beginning, as well as in various parts of the HTTP response body.

The solution for me was to strip out the null characters from the response before sending it to the client.

In PHP you can do this by (for example):

<?
    $YOUR_RESPONSE = file_get_contents('http://example.com/sloppy_page');
    //If you already have your response stored in field, then just use that field
    echo str_replace("\0", '', $YOUR_RESPONSE);
?>

Hope I can help someone with this!

Upvotes: 2

wajiw
wajiw

Reputation: 12269

IE does cache similar ajax requests. The workaround I always use is to add a random variable to ajax calls that need caching turned off:

$('#txtHint').hide();

    function showReviews(str) {
        $.ajax({

            url: "dropsearch.php",
            cache: false,
            type: "post",
            data: "q=" + str+"&r="+Math.random(),
            dataType: "html",

    success: function(data) {
      $('#txtHint').append(data);
      $('#txtHint').show();
      window.alert(data);
    }
  });
}

Also, like @Jacob said you have no need to select what type of request $.ajax is using.

Upvotes: 4

Related Questions