我无法通过xhr发送帖子请求

时间:2017-09-08 17:21:55

标签: javascript ajax xmlhttprequest

我想将数字发送到文件并从文件接收数据。 在文件loader.php中我做了这个回答" echo json_encode($ _ REQUEST)"请求,以确保来文件或不,但我会看到空数组。帮助我,请。我不知道出了什么问题?

document.addEventListener("DOMContentLoaded", function(event) {
var inProgress = false;

var startFrom = 10;

window.addEventListener('scroll', function() {
    var scrollHeight = Math.max(
        document.body.scrollHeight, document.documentElement.scrollHeight,
        document.body.offsetHeight, document.documentElement.offsetHeight,
        document.body.clientHeight, document.documentElement.clientHeight
    );

    if(window.pageYOffset + window.innerHeight >= scrollHeight - 200 && 
!inProgress) {


        let request = obj => {
            return new Promise((resolve, reject) => {
                let xhr = new XMLHttpRequest();
                xhr.open(obj.method || "GET", obj.url);
                if (obj.headers) {
                    Object.keys(obj.headers).forEach(key => {
                        xhr.setRequestHeader(key, obj.headers[key]);
                    });
                }

                xhr.onload = () => {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        resolve(xhr.response);
                    } else {
                        reject(xhr.statusText);
                    }
                };
                xhr.onerror = () => reject(xhr.statusText);
                xhr.send(obj.body);
            });
        };

        request({url: "/proc-service/loader.php", method: "POST", body: "1"})
            .then(data => {
                let tasks = JSON.parse(data);
                console.log(tasks);
                let html = "";
                tasks.forEach(task => {
                    html +=
                        "<li id=\"content__list\">" +
                        "<span>" + task.id + "</span> " +
                        "<span>" + task.title + "</span>" +
                        "</li>";
                });
                document.getElementById("content").innerHTML = html;
            })
            .catch(error => {
                console.log(error);
            });

    }
});
});

0 个答案:

没有答案