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