Question

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);
        }
    }
}
Was it helpful?

Solution

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

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top