简体   繁体   English

ESP8266未收到HTTPRequest

[英]ESP8266 not receiving HTTPRequest

I can't seem to receive Javascript's HTTPRequest. 我似乎无法收到Javascript的HTTPRequest。 I get this HTTP/1.1 200 OK but I cannot seem to get the URL that I sent over. 我得到了HTTP/1.1 200 OK同意,但似乎无法获得发送过来的URL。 I just want the link that I am sending over on my webpage. 我只想要我的网页上发送的链接。

Here is my javascript: 这是我的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: ESP8266在void循环中:

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). 您仅从ESP上的index.php脚本中读取响应的第一行,通常是您所看到的HTTP状态代码,而没有别的(除非您没有发布其他代码)。 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. 您需要阅读整个HTTP消息以获取响应,特别是正文-假设index.php使用正文返回所需的数据。 You can find a description of the HTTP message structure here: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html 您可以在此处找到HTTP消息结构的描述: https : //www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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