我得到“对象的状态必须为打开状态”。

时间:2019-11-12 16:22:37

标签: javascript php ajax

我尝试执行AJAX请求,但是出现这种错误,应该打开ajax调用。但是它已经打开了。尝试发送XMLHttpRequest标头,但出现此类错误。伙计们,救救我吧!

let del_btn = document.querySelectorAll('.delete');
for(let i = 0; i < del_btn.length; i++)
{
del_btn[i].addEventListener('click', function(){
        let xhr = new XMLHttpRequest();
        let book_title = del_btn[i].parentElement.children[1].children[1].children[0].children[0].textContent;
        xhr.onreadystatechange = function()
        {
            if(xhr.readyState == 4 && xhr.status == 200)
            {
                if(this.responseText.includes('Deleted'))
                {
                    let books = this.responseText.split('.')[1];
                    if(books == '0')
                    {
                        del_btn[i].parentElement.parentElement.innerHTML = 'You have no books in your list. Go to the ' + ' ' + '<a href="index.php"> main page </a>' + ' ' + 'and start your ride!';
                    }
                    else
                    {
                        del_btn[i].parentElement.parentElement.removeChild(del_btn[i].parentElement);
                        document.querySelector('.amount-b').textContent = 'Количество книг: ' + books;
                    }
                }

            }
        }
        xhr.open("POST", "../operations/delete_book.php", true);
        xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send('title=' + book_title);
    }, false);
}```

0 个答案:

没有答案