943,678 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 13th, 2008
0

AJAX vs IE issue

Expand Post »
Hello, I am having problems with Internet Explorer (as always) and AJAX.

My code works with Firefox, but the page returns errors when trying to execute this script in Explorer:

<script type="text/javascript" language="javascript">
   var xmlHttp = false;

   function createPostRequest(url, parameters) {
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
      
      	xmlHttp.onreadystatechange = alertContents;
      	xmlHttp.open('POST', url, true);
      	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      	xmlHttp.setRequestHeader('Content-length', parameters.length);
      	xmlHttp.setRequestHeader('Connection', "close");
      	xmlHttp.send(parameters);
   }

   function alertContents() {
      if (xmlHttp.readyState == 4) {
         if (xmlHttp.status == 200) {
            result = xmlHttp.responseText;
            document.getElementById('result').innerHTML = result;   
	    document.getElementById('submitbutton').disabled = false;  //re-enable the trigger       
         } else {
            alert("There was a problem with the request.");
         }
      }
   }
   
   function manufacture(obj) {
      document.getElementById('submitbutton').disabled = true;   //disabling the trigger
      var poststr = "out1=" + escape(encodeURI(value1)) + "&out2=" +  escape(encodeURI(value2)) + "&out3=" +  escape(encodeURI(value3)); //there are more variables here, but they are generated with PHP (and the string is created as it should both in IE and FF)

      createPostRequest("manufacture.php", poststr);
   }
</script>

Anyone can see what is wrong straight away?
Last edited by Ciubhran; Aug 13th, 2008 at 9:45 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ciubhran is offline Offline
7 posts
since Aug 2008
Aug 13th, 2008
0

Re: AJAX vs IE issue

I believe xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); is outdated. See how W3Schools suggests implementing for IE. Also, read more about the argument here.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Aug 13th, 2008
0

Re: AJAX vs IE issue

That wasn't it, unfortunately.

It now supports all kinds of different IE things:

var XMLHTTPREQUEST_MS_PROGIDS = new Array(
  	"Msxml2.XMLHTTP.7.0",
  	"Msxml2.XMLHTTP.6.0",
 	"Msxml2.XMLHTTP.5.0",
  	"Msxml2.XMLHTTP.4.0",
  	"MSXML2.XMLHTTP.3.0",
  	"MSXML2.XMLHTTP",
  	"Microsoft.XMLHTTP"
   );

   var xmlHttp = null;

   function makePOSTRequest(url, parameters) {
	if (window.XMLHttpRequest != null) {
    		xmlHttp = new window.XMLHttpRequest();
	}
  	else if (window.ActiveXObject != null) {
    		// Must be IE, find the right ActiveXObject.
   		var success = false;
    		for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++) {
			try {
        			xmlHttp = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
        			success = true;
      			}
      			catch (ex) {}
    		}
  	}
	else {
		alert("Your browser does not support AJAX.");
		return xmlHttp;
	}
 
      	xmlHttp.onreadystatechange = alertContents;
      	xmlHttp.open('POST', url, true);
      	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      	xmlHttp.setRequestHeader('Content-length', parameters.length);
      	xmlHttp.setRequestHeader('Connection', "close");
      	xmlHttp.send(parameters);
   }
	
   function alertContents() {
      if (xmlHttp.readyState == 4) {
         if (xmlHttp.status == 200) {
            result = xmlHttp.responseText;
            document.getElementById('result').innerHTML = result;   
	    document.getElementById('submitbutton').disabled = false;         
         } else {
            alert("There was a problem with the request.");
         }
      }
   }

But the problem remains.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ciubhran is offline Offline
7 posts
since Aug 2008
Aug 14th, 2008
0

Re: AJAX vs IE issue

As always: how does it fail? What happens, how does that differ from your expectations, what debugging information do you get? (Tip: go to Tools > Internet Options, Advanced tab, and make sure "Browsing: Display a notification about every script error" is on and "Browsing: Disable script debugging (Internet Explorer)" and "Browsing: Disable script debugging (Other)" are off.)
Reputation Points: 18
Solved Threads: 4
Light Poster
chaosprime is offline Offline
46 posts
since Jul 2008
Aug 14th, 2008
0

Re: AJAX vs IE issue

Object doesnt support this property or method.

If my inferior debugging skills serve me right, its on the same line as send().
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ciubhran is offline Offline
7 posts
since Aug 2008
Aug 14th, 2008
0

Re: AJAX vs IE issue

If you can't tell by matching the reported line number with your code, then insert alert()s for debugging. For instance, if you put one before and one after the send() call, and the first alert() fires and the second doesn't, then you know it was in fact the send() call. If neither alert fires, it's earlier. If both alerts fire, madness ensues.
Reputation Points: 18
Solved Threads: 4
Light Poster
chaosprime is offline Offline
46 posts
since Jul 2008
Aug 14th, 2008
0

Re: AJAX vs IE issue

I just spent some time fooling around with this and not realizing that my path to my server-side script wasn't being found by the server (on my local computer)...when i fixed that it just worked -- I don't know what I did. It's working on my computer for IE 7, FF 3, Opera 9.51.

Server side script:
remote.php
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. print "pizza";
  3. ?>

Fiddled with Ajax page:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. var XMLHTTPREQUEST_MS_PROGIDS = new Array(
  6. "Msxml2.XMLHTTP.7.0",
  7. "Msxml2.XMLHTTP.6.0",
  8. "Msxml2.XMLHTTP.5.0",
  9. "Msxml2.XMLHTTP.4.0",
  10. "MSXML2.XMLHTTP.3.0",
  11. "MSXML2.XMLHTTP",
  12. "Microsoft.XMLHTTP"
  13. );
  14.  
  15. function makePOSTRequest( url, parameters ) {
  16. var xmlHttp = null;
  17. if ( window.XMLHttpRequest != null ) {
  18. xmlHttp = new window.XMLHttpRequest();
  19. } else if ( window.ActiveXObject != null ) {
  20. // Must be IE, find the right ActiveXObject.
  21. var success = false;
  22. for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i ++) {
  23. try {
  24. xmlHttp = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
  25. success = true;
  26. } catch (ex) {}
  27. }
  28. } else {
  29. alert( "Your browser does not support AJAX." );
  30. return xmlHttp;
  31. }
  32. xmlHttp.onreadystatechange = alertContents;
  33. xmlHttp.open( 'POST', url, true );
  34. xmlHttp.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
  35. xmlHttp.setRequestHeader( 'Content-length', parameters.length );
  36. //xmlHttp.setRequestHeader('Connection', "close");
  37. xmlHttp.send( parameters );
  38. }
  39.  
  40. function alertContents() {
  41. // alert( this.status );
  42. if ( this.readyState == 4 ) {
  43. //alert( this.responseText );
  44. if ( this.status == 200 ) {
  45. result = this.responseText;
  46. // document.getElementById('result').innerHTML = result;
  47. // document.getElementById('submitbutton').disabled = false;
  48. alert( result );
  49. } else {
  50. //alert( this.getAllResponseHeaders() );
  51. alert( "There was a problem with the request." );
  52. }
  53. }
  54. }
  55. </script>
  56. </head>
  57. <body>
  58. <a href="javascript:makePOSTRequest('ajax/remote.php','name=value')">Click me please</a>
  59. </body>
  60. </html>

Look it over, maybe you'll see what I did

Ciao
Last edited by langsor; Aug 14th, 2008 at 9:03 pm.
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Aug 15th, 2008
0

Re: AJAX vs IE issue

I pretty much copied your example, still doesn't work.

And now my debugger says its on "open()"...

But, adding alerts() on places around it, the place of error changes to the alert...

So I don't know what is up with the IE script debugger.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ciubhran is offline Offline
7 posts
since Aug 2008
Aug 15th, 2008
0

Re: AJAX vs IE issue

PROBLEM SOLVED!

God, that was so easy.

The error was that I never declared 'result' as a variable.

It was:

"result = this.responseText"

which I simply changed to

"var result = this.responeText"

The variable isnt even needed, don't know why it is there.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ciubhran is offline Offline
7 posts
since Aug 2008
Aug 15th, 2008
0

Re: AJAX vs IE issue

Found another problem now.

If I declare "var xmlHttp" as a global, it only works in FF. If I declare it inside of makePOSTRequest (like the above example), then it only works in IE.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ciubhran is offline Offline
7 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Troubleshoot script
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Ajax and server relation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC