Jump to content

XMLHttpRequest return status as 0


sourish_khan

Recommended Posts

Hi Ajax Experts!  :)

 

I'm posed with a different kind of issue using XMLHttpRequest.

I have an html page with a javascript is hooked on with "onload" event. A javascript method "post_to_url()" is invoked on page load.

My code goes as follows...

 

<html>
<head>
<script type="text/javascript">
function post_to_url() { 
//var poststr = "j_user=" + escape(encodeURI(username)) +"&j_password=" + escape(encodeURI(document.getElementById("j_password").value));
//var poststr = "j_user=" + escape(encodeURIComponent("tester")) +"&j_password=" + escape(encodeURIComponent("password"));
var poststr = "j_user=tester&j_password=password";
postRequestToEP('http://myportalserver.com:50000/irj/portal', poststr);
} 

function postRequestToEP(url, parameters) {
      http_request = false;
      if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");  //IE5, IE6
            } catch (e) {
			http_request = false;
		}
         }
      } 
  
  if (!http_request && window.XMLHttpRequest) { // Mozilla, Safari,...
         try {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
         		       // set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	 } catch(e) {
		http_request = false;
	 }
      } 
    if (!http_request) {
       alert("Cannot create XMLHTTP instance");
       return false;
    }
    http_request.onreadystatechange = WPSLogin;
    //http_request.open("GET", url+"?"+parameters, true);
http_request.open("POST", url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
//http_request.send();
}

function WPSLogin() {
if (http_request.readyState == 4) {
	if (http_request.status == 200) {
                    alert('Successfully logged in to Portal Server!');
                } else {
                   alert('Problem in login to Portal Server!');
                }
}
}
</script>
</head>
<body onload='post_to_url()'>
Test
</body>
</html>

The code executes fine on Internet Explorer 8. But on Firefox/IE6/IE7, I am getting the value of http_request.status = 0, though there is no issue in connecting to the server. Please refer to attached image for clarity.

 

Upon a bit of googling, I found out that this can occur in case the sites are hosted in different domain due to "Same Domain Policy". But I guess, this is not relevant in my case.

 

Can anyone please help me out figuring out the issue in my code. Look forward to all your valuable inputs.

 

Best regards,

Sourish

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.