简体   繁体   English

xmlhttprequest responsetext为null

[英]xmlhttprequest responsetext is null

I'm trying to make basic HTML Server connection, therfore I want to call and JS function which should call an PHP file just schoing "hello world". 我试图建立基本的HTML Server连接,然后再调用JS函数,该函数应调用一个“ hello world”来调用PHP文件。 Everything is working so far but the response I get seems to be null. 到目前为止,一切正常,但是我得到的响应似乎是无效的。 I've read through various tutorials but I did not find an answer, hope someone can help me. 我已经阅读了各种教程,但没有找到答案,希望有人可以帮助我。

            var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status==200 && xhttp.responseText!=null) {
                alert("Responestext: " + xhttp.responseText + "  ENDE");
            }else if(xhttp.status==403){
                alert("forbidden");
            }else if(xhttp.status==404){
                alert("not found");
            }else if(xhttp.responseText==null) {
                alert("response text = null");
            }else{
                alert("Error");
            }
        };
        xhttp.open("GET", "http://URL/fahrgemeinschaft/login.php", true);
        xhttp.send(null);

I expect the output to be "Responsetext: hello world ENDE" but all I get is "Error". 我希望输出为“ Responsetext:hello world ENDE”,但我得到的只是“错误”。 I get two alert boxes saying "Error" as well. 我有两个警告框,上面也说“错误”。

The problem is your onreadystatechange handler is called for every ready state change event, not just when it is done. 问题是您的onreadystatechange处理程序是为每个就绪状态更改事件而不是仅在完成时调用的。

You should skip the events that are not done: 您应该跳过未完成的事件:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState != XMLHttpRequest.DONE) {
        return;
    }
    if (xhttp.status==200 && xhttp.responseText!=null) {
        alert("Responestext: " + xhttp.responseText + "  ENDE");
    }else if(xhttp.status==403){
        alert("forbidden");
    }else if(xhttp.status==404){
        alert("not found");
    }else if(xhttp.responseText==null) {
        alert("response text = null");
    }else{
        alert("Error");
    }
};
xhttp.open("GET", "http://messenger.hopto.org:8080/fahrgemeinschaft/login.php", true);
xhttp.send(null);

Or to make it easier on yourself, so long as you don't need to support obsolete browsers, just use the onload event. 或者使您自己更轻松,只要您不需要支持过时的浏览器,只需使用onload事件即可。

var xhttp = new XMLHttpRequest();
xhttp.onload = function() {
    if (xhttp.status==200 && xhttp.responseText!=null) {
        alert("Responestext: " + xhttp.responseText + "  ENDE");
    }else if(xhttp.status==403){
        alert("forbidden");
    }else if(xhttp.status==404){
        alert("not found");
    }else if(xhttp.responseText==null) {
        alert("response text = null");
    }else{
        alert("Error");
    }
};
xhttp.open("GET", "http://messenger.hopto.org:8080/fahrgemeinschaft/login.php", true);
xhttp.send(null);

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

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