XmlHTTPrequest will not open ("GET", url, true); I am a myth! Php

I am trying to open a url, but I do not understand why this did not work. The code is indicated and explained below. Any help would be greatly appreciated.

An 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 is 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!");
        }

Warning ("No road!"); indicates that the code did not complete this point:

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

This means that if (myDelete) {works. The code goes through this step and for some reason stops here: myDelete.open ("GET", query, true); It will not open the URL. I am not sure what the problem is.

Edit: Here is 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
source share
3

.status == 4 .readyState?

+2

this, XMLHttpRequest.open() , .

+3

All xmlHTTPRequests are associated with the same origin policy. Perhaps your problem.

You can learn more about this at http://en.wikipedia.org/wiki/Same_origin_policy

0
source

Source: https://habr.com/ru/post/1705196/


All Articles