xmlHTTPrequest won't open ("GET" , url, true); I'm miffed! PHP

11,062

Solution 1

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

Solution 2

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

Share:
11,062
hoyt.dev
Author by

hoyt.dev

We focus on the software so that you can focus on building more quality content: http://clientbucket.com http://twitter.com/clientbucket http://www.facebook.com/clientbucket

Updated on June 05, 2022

Comments

  • hoyt.dev
    hoyt.dev almost 2 years

    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);
            }
        }
    }