我想從網頁中檢索數據,然後將其顯示在我的網頁上,沒有什麼花哨的atm只是顯示它,所以它可以讀取,但我不知道如何做到這一點,這是我迄今爲止(也對不起,如果我不這樣做的格式正確我仍然新本):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是在一個名爲拉一個單獨的文件。 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);
}
}
}
您的JavaScript需要在''標籤。 – Bardicer
這是格式化在這裏嗎?如果是這樣的提示,我會記住,如果它關於實際的代碼,我有他們在不同的文件,並引用JS使用 – CallumRob
你有什麼嘗試,doesn不工作?你知道如何使用Javascript將元素插入DOM嗎?或者你沒有從Ajax調用中獲取數據?如果你能夠使用它,JQuery可能有助於簡化你的代碼。 – Phil