javascript - XMLHttpRequest可以获取数据显示在网页上

标签 javascript html ajax xmlhttprequest

我正在尝试从网页检索数据,然后将其显示在我的网页上,没有什么花哨的自动取款机只是显示它以便可以读取它,但是我不知道如何执行此操作,这就是我到目前为止所拥有的(也很抱歉,如果我没有正确完成格式化,我对此还是新手):

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title> Night Out In Glasgow!!</title>
<link rel="stylesheet" type="text/css" href="StyleSheet.css">
<script src="pull.js"></script>
</head>
<body> 
    <form action = "">
        <p><button type = "button" onclick ="getData()">Get The Data</button>
        </p>
    </form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

</body>

这就是我的 JS,它位于一个名为 pull.js 的单独文件中,我已在 HTML 中链接到该文件,希望这可以消除原始帖子中的任何混淆。

    /*jslint node: true, browser: true */

    "use strict";
    /*jslint node: true, browser: true */

    "use strict";

    function getData(){
        var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST","http://ratings.food.gov.uk/OpenDataFiles/FHRS776en- GB.xml");
        xmlhttp.onreadystatechange = checkData;
        xmlhttp.send(null);
        function checkData() {
           if(xmlhttp.status == 4){   
           if(xmlhttp.status == 200){
            //We've got a response
            alert(xmlhttp.responseXML);
             }
           }
           else{
               //Somethings went wrong
               alert("Error: " + xmlhttp.status + ": " +xmlhttp.statusXML);
                  }
                }
              }

最佳答案

按以下顺序尝试:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","...");
xmlhttp.onreadystatechange = {
       if(xmlhttp.status == 4){   
       if(xmlhttp.status == 200){
       ...
};
xmlhttp.send();

关于javascript - XMLHttpRequest可以获取数据显示在网页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33923964/

相关文章:

javascript - Mocha + Babel + React 模块

javascript - 未处理的拒绝(类型错误): Cannot read property 'children' of undefined

javascript - 我的预加载器没有褪色,并且一直在无限循环中滚动

php - 如何在 PHP 中返回 ajax 调用的错误状态?

PHP如何使用ajax进行表单提交

javascript - 如何从异步调用返回响应?

javascript - MongoDB 对象不更新

javascript - 使用kineticjs和tweens实现持久动画

javascript - HTML JS 注入(inject)表单提交

javascript - 如何在具有相同功能的类似 div 上复制功能