简体   繁体   中英

ESP8266 not receiving HTTPRequest

I can't seem to receive Javascript's HTTPRequest. I get this HTTP/1.1 200 OK but I cannot seem to get the URL that I sent over. I just want the link that I am sending over on my webpage.

Here is my javascript:

jQuery(function($) {$("button").click(function(){
    console.log(this.id);
    document.getElementById("direction").innerHTML = this.id + " is pressed.";

    newurl = 'index.php?btn='+ this.id+"?key="+user.key; 
    sendHttpRequest(newurl);
    });
});

function sendHttpRequest(update){
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
          console.log(xhttp.responseText);
        }
    }
    xhttp.open("GET",update,true);
    xhttp.send(null);

    console.log(update);
}

ESP8266 in void loop:

WiFiClient client;
String url = "/index.php?";

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
             "Host: " + host + "\r\n" + 
             "Connection: close\r\n\r\n");
  Serial.println("Request Sent");


  String request = client.readStringUntil('\r');
  Serial.println("headers received");
  Serial.println(request);

You're only reading the first line of the response from the index.php script on the ESP, which will generally be the HTTP status code you are seeing, and nothing else (Unless there's some other code you haven't posted). You need to read the entire HTTP message to get the response, specifically the body - Assuming index.php uses the body to return the data you need. You can find a description of the HTTP message structure here: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM