【问题标题】:ESP8266 not receiving HTTPRequestESP8266 未收到 HTTPRequest
【发布时间】:2023-03-12 19:20:01
【问题描述】:

我似乎无法接收 Javascript 的 HTTPRequest。我收到了这个HTTP/1.1 200 OK ,但我似乎无法获得我发送过来的 URL。我只想要我在网页上发送的链接。

这是我的 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 在 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);

【问题讨论】:

    标签: javascript arduino esp8266


    【解决方案1】:

    您只是从 ESP 上的 index.php 脚本读取响应的第一行,这通常是您看到的 HTTP 状态代码,没有其他内容(除非您还没有发布其他代码)。您需要阅读整个 HTTP 消息以获取响应,特别是正文 - 假设 index.php 使用正文返回您需要的数据。您可以在此处找到对 HTTP 消息结构的说明:https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      相关资源
      最近更新 更多