Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ajax not work in ie

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
Can anyone see why this doesn`t work in IE (7 or 8, not tried others). I have the check at the bottom that looks for XMLHttpRequest object. It works in other browsers.

Thanks


Code:
<script type="text/javascript">
var xmlhttp

function removeProduct(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="removeproduct.asp";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  document.getElementById("basketarea").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
</script>
 
What debugging have you done? For example:

- Does your call to GetXmlHttpObject() return anything in IE?

- Does your stateChanged() function ever get called by IE?

Saying simply that it "doesn't work" is not very informative.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
There aren`t any errors in the firefox error console but it works properly in there. IE doesn`t come up with an error, how do I go into more depth in IE?
 
In the absence of a decent debugger for IE (such as the script debugger that comes with MS office), you could try the old fashioned method of putting alert statements in various places.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
I like to use this for my AJAX apps, it's never let me down yet!

Code:
// AJAX FUNCTION //
    
function makeRequest(url, parameters, myfunc) {

    var http_request;
    
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
    }

    if (!http_request) {
            alert('AJAX - Giving up :( Cannot create an XMLHTTP instance');
            return false;
    }
        
    http_request.onreadystatechange = function(){
           
            
            if (http_request.readyState == 4) {
                if(http_request.status && http_request.status == 200) {                   
                    // Call return function
                    myfunc(http_request.responseText);                                               
                } 
                else {
                    alert('There is a problem with AJAX, please contact support.'); 
                    return false;
                }
            }  
    };           

    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
      
}

// END OF AJAX FUNCTION //

This enables you to easily call server side scripts with query strings and receive the returned HTML text to a function of your choosing.

it uses the following syntax

makeRequest(url of script to call, query string parameters, function to call with returned HTML)

You call it simply like this...
Code:
// build query string
var myVar1 = "some value";
var myVar2 = "another value";

var QueryString = "var1=" + myVar1 + "&var2=" + myVar2;

// set script to call
var myScript = "[URL unfurl="true"]http://www.mydomain.com/cgi-bin/myScript.pl";[/URL]

// make AJAX call
makeRequest(myScript,QueryString,disp_result);

Then to process the result....
Code:
function disp_result(html)
{
   document.getElementById("basketarea").innerHTML=html;
}

This is just a simple usage but you can see how using the makeRequest script gives you easy power to build simple AJAX apps.

It's also non-destructive so can be used to make multiple consecutive asynchronous AJAX calls without conflict.

Hope it helps. :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top