weixin_33727510 2018-04-11 15:23 采纳率: 0%
浏览 358

为什么readyState == 0不起作用

So my problem is that when I'm using if(this.readyState == 0) It doesn't do its job. I hava e script that loads form with ajax and I have the option to close the form, but then i realised that if i want to open that form again without making new ajax request i would need somekind of variable for it. I have variable called "isOpen" and when xhr readyState is 0 i want it to check if isOpen is true and in case if its true it aborts xhr and shows the form data. Right now it doesn't seem to work as i expected it to.

Here is my whole function for handling this AJAX request.

function postPlacesForm() {
var yhteys = new XMLHttpRequest();
yhteys.onreadystatechange = function() {
    var isOpen = false;
    if(this.readyState === 0) {
        if(isOpen === false) {

        }
        else if(isOpen === true) {
            document.getElementById("places").style.display = "block";
            yhteys.abort();
        }
    }
    if(this.readyState === 4 && this.status === 200) {
        document.getElementById("places").innerHTML = this.responseText;
        document.getElementById("places").style.display = "block";
        document.getElementById("ajax_close").style.display = "block";
        isOpen = true;
    }

};
yhteys.open("POST", "/js/ajax_inc/forms/postplaces.php", true);
yhteys.send();

}

  • 写回答

2条回答 默认 最新

  • weixin_33682790 2018-04-11 15:32
    关注

    It looks that you don't understand the readyState property yet.

    • 0 UNSENT Client has been created. open() not called yet.
    • 1 OPENED open() has been called.
    • 2 HEADERS_RECEIVED send() has been called, and headers and status are available.
    • 3 LOADING Downloading; responseText holds partial data.
    • 4 DONE The operation is complete.

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置