yusdddd_ 2022-01-14 12:06 采纳率: 94.1%
浏览 73
已结题

AJAX请求返回XML出现错误,如何解决?

index.html:21 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.
at HTMLInputElement.document.querySelector.onclick (http://127.0.0.1:8081/chattest/index.html:21:13)

百度和谷歌查了个遍还是不知道怎么解决。

源代码:

    <h3>ajax请求</h3>
    <input type="button" value="ajax请求返回xml">

    document.querySelector('input').onclick = function(){
        var xhr = new XMLHttpRequest();
        xhr.open = ('post' , 'backxml.php');
        xhr.onreadystatechange = function(){
            if(xhr.readyState==4&&xhr.status==200){
                console.log(xhr.responseText);
            }
        }
        xhr.send(null);
    }
```').


```php
    $xmlString = file_get_contents('person.xml');
    echo $xmlString;

照着B站AJAX教程打的代码,跟那个一模一样,但是我的就是运行不了,检查了十几二十遍。TAT

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-01-14 13:57
    关注

    =号去掉,要调用xhr对象的oen方法后才能send

    img

    建议onreadystatechange该这样,要不服务器端有问题就会和没反应一样,只能开浏览器调试工具看网络请求

    <h3>ajax请求</h3>
    <input type="button" value="ajax请求返回xml">
    
    <script>
        document.querySelector('input').onclick = function () {
            var xhr = new XMLHttpRequest();
            xhr.open ('post', 'backxml.php');
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        console.log(xhr.responseText);
                    } else 
                        alert('服务器有问题\n' + xhr.responseText)
                }
            }
            xhr.send(null);
        }
    
    </script>
    
    

    img


    有其他问题可以继续交流~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月22日
  • 已采纳回答 1月14日
  • 创建了问题 1月14日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来