views:

2050

answers:

3

I've been trying to get a url to open but I'm miffed as to why this hasn't worked. The code is listed and explained below. Any help will be deeply appreciated.

The object:

function getXMLHTTPRequest() {
   var req =  false;
   try {
      /* for Firefox */
      req = new XMLHttpRequest(); 
   } catch (err) {
      try {
         /* for some versions of IE */
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (err) {
         try {
            /* for some other versions of IE */
            req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (err) {
            req = false;
         }
     }
   }

   return req;
}

The object is called like this:

<script type="text/javascript">
var myDelete = new getXMLHTTPRequest();
</script>

Now here's what I want to do:

function removeArticle(id) {

    if (myDelete) {

     try {
      var deletUrl = "delete.php";
      var query = deletUrl + "?theid=" + id;
      myDelete.open("GET", query, true);
      myDelete.onreadystatechange = removeArticleResponse;
      myDelete.send(null);
     } catch (e) {
      alert ("Unable to connect to the server:\n" + e.toString());
     }
    } else {
     alert ("Bad! Very BAD!");
    }
}

When I do this:

  if (myDelete.open("GET", query, true)) {
  myDelete.onreadystatechange = removeArticleResponse;
  myDelete.send(null);
  } else {
   alert ("No road!");
  }

The alert("No road!"); shows me that the code doesn't execute passed this point:

if (myDelete.open("GET", query, true)) {

This means that the if (myDelete) { works. The code passes this stage and for some reason stops here: myDelete.open("GET", query, true); It won't open the url. I'm not sure what the problem is.

Edit: Here's the function used to access the server response:

function removeArticleResponse () {
    if (myDelete.status == 4) {
     if (myDelete.status == 200) {
         try {
          response = myDelete.responseText;
          document.getElementById('displaynewsletterarticleresult').innerHTML = response;
         } catch(e) {
          alert("An error occured while reading the response:" + e.toString());
         }
     } else {
     alert ("An error occured when attempting to retrieve the data:\n" + myDelete.statusText);
     }
    }
}
+3  A: 

According to this, XMLHttpRequest.open() has no return value, so your check will always fail.

Tom Haigh
Thanks for the link. But this means I'm back to square one, not knowing what the problem is in the first place. I get no response from the script I'm sending the parameters to. No error message, nothing. I know the id was passed but after that I'm not sure what's happened. Any ideas? Thanks, a lot!
play
I've edited the question to show the function used to access the response from the server.
play
Maybe using a debugger like firebug to try to work out what is going on would help? Or just put alert statements everywhere so you can follow the logic
Tom Haigh
Thanks. Problem solved. I'll look into firebug.
play
+2  A: 

In your response function, do you mean to check .status == 4 instead of .readyState?

Soldarnal
Thanks! That was it. I now get a response. That was a bone headed mistake.
play
A: 

All xmlHTTPRequests are bound to the same origin policy. Maybe that's your issue.

You can read more about it at http://en.wikipedia.org/wiki/Same_origin_policy

L. Cosio