Monday, February 8, 2010

Problem using AJAX call in IE6 and IE7

IE6 & IE7 makes problem when use xmlhttprequest for ajax

At first time when I used core ajax using xmlhttprequest. Using this technique we sent parameter as querystring through the url,like this,

function GetEmailId()
{
var url="http://server.com/test.aspx";
url=url+"&sid=test";
xmlhttp.onreadystatechange=statechangedLogin;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
function statechangedLogin()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.responseText=="Login")
{
//write your code
}
}
}

For the first time it is working fine,but when you try to second time in the same browser(IE6) its not work.

This is becuse Internet Explorer will also cache dynamic pages and this is a problem because the URL of the page may not change but the content will.A workaround for this situation can be achieved by adding a unique time stamp or random number, or possibly both, typically using the Date object and/or Math.random() such as url=url+"&sid=test"+Math.random();.

As for example:
function GetEmailId()
{
var url="http://server.com/test.aspx";
url=url+"&sid=test"+Math.random();
xmlhttp.onreadystatechange=statechangedLogin;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}


For simple document request the query string delimiter '?' can be used, or for existing queries a final sub-query can be added after a final '&' – to append the unique query term to the existing query. The downside is that each such request will fill up the cache with useless (never reused) content that could otherwise be used for other cached content (more useful data will be purged from cache to make room for these one-time responses).

A better workaround can be achieved by adding meta tags to dynamic pages in order to make them no-cachable:

<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />

For more info please visit:
http://en.wikibooks.org/wiki/JavaScript/XMLHttpRequest
http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspx
http://stackoverflow.com/questions/1229821/problem-using-ajax-call-in-ie6-and-ie7-i-am-asking-the-same-question-again-ca

2 comments:

Anonymous said...

thanks a lot, it helps me a lot

Unknown said...

welcome,your feedback also helps me to get encourage.

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...