0

I have a little webserver on a microcontroller/WIFI module. Then I create on my PC an HTML file which is a simple button on a page (code below) to send a GET request over the internet to my server via Ajax using a browser. When I click the "SEND" button, I can see the entire HTTP request that arrived to the board and shows that all on the screen via serial port. So the microcontroller gives the following response:

HTTP/1.1 200 OK Content-Type: application/json {"fixedNotice":"9"}

The last line is the text of the response, I am trying to show this response on text input, but it's not working, the browser seems to be not receiving any reply to the request. And I am looking at the serial port terminal and the response is being given...

<!DOCTYPE html>

<html>

    <head>
        <title>BUTTON OVER INTERNET TEST</title>
        <meta charset="UTF-8"/>
    </head>

    <body>

        <input type="text" id="result" size="50">
        <input type="button" onclick="Send()" value="SEND">

        <script>

            function Send() 
            {
                var XHR = new XMLHttpRequest();

                XHR.open("GET", "http://181.148.227.41:8383/internet_test", true);

                XHR.onreadystatechange = function() 
                {
                    if (this.readyState == 4 && this.status == 200) 
                    {
                        document.getElementById("result").value = this.responseText;
                        return;
                    }
                };

                XHR.send();
            }

        </script>

    </body>

</html>

Why I am not receiving the response for the Ajax/GET request? Is that something related to CORS? That IP shown is fake.

Pankwood
  • 1,799
  • 5
  • 24
  • 43
abomin3v3l
  • 167
  • 1
  • 10
  • 1
    "the browser seems to be not receiving any reply to the request" — How are you determining this? Are you looking at the Network tab of the developer tools? – Quentin Dec 03 '19 at 17:13
  • 1
    "Is that something related to CORS?" — Is there a CORS related error message in the Console of the developer tools? – Quentin Dec 03 '19 at 17:13
  • 1
    "And I am looking at the serial port terminal and the response is being given" — What does that response look like? – Quentin Dec 03 '19 at 17:14
  • You mention "Then I create on my PC an HTML file", which leads me to believe you are attempting to use XMLHttpRequest from a file served using the `file://` protocol, which will not work (except with certain browsers started with certain flags). Basically, you almost assuredly have a CORS issue if your HTML file containing that code is not running from the same web server. – Heretic Monkey Dec 03 '19 at 17:19
  • @Quentin I was not looking at the network tab, when I click the SEND button, the Status column inside this tab shows (pending) and after arounf 500ms it shows 200. In the response of the microcontroller, the header is only "HTTP/1.1 200 OK, NEW LINE, Content-Type: application/json" and the data content is to be "{"fixedNotice":"9"}"... I can't see any message about CORS in the Network tab. – abomin3v3l Dec 03 '19 at 17:33
  • 1
    Look at the **Console** for CORS related errors, not the Network tab. – Quentin Dec 03 '19 at 17:35
  • @Quentin For each request I get one error and one warning shown in the Console... – abomin3v3l Dec 03 '19 at 17:43
  • Access to XMLHttpRequest at 'http://181.148.227.41:8383/internet_test' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. – abomin3v3l Dec 03 '19 at 17:44
  • Cross-Origin Read Blocking (CORB) blocked cross-origin response http://'181.148.227.41:8383/internet_test with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details. – abomin3v3l Dec 03 '19 at 17:45
  • 1
    "has been blocked by CORS policy" — Well, there you go. – Quentin Dec 03 '19 at 17:52
  • @Quentin Thanks very much. Now I know what to look for / study. – abomin3v3l Dec 03 '19 at 17:55
  • @Quentin I have only one dumb question: "requested resource" is to be the microcomntroller response in my case? The header sent by the microcontroller in response to the request? – abomin3v3l Dec 03 '19 at 17:59
  • 1
    The resource is the entire HTTP response. – Quentin Dec 03 '19 at 17:59

0 Answers0