1

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

2
  • Any reason why you're specifying an xhr property in your call to $.ajax? That shouldn't be needed. The ajax function figures out the proper request object for you. Commented Jan 18, 2011 at 23:04
  • Just a quick question, why are you setting the xhr to the same thing jQuery's ajax defaults to? api.jquery.com/jQuery.ajax Probably not the solution, but it seems rather redundant. Commented Jan 18, 2011 at 23:06

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This makes no difference, I understand that IE has problems with cache in ajax requests, which is why I though adding cache: false, would do the trick, but no joy. I specified the xhr property just to see if that would make a difference, but obviously it doesn't.
2

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!

Comments

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.