2015-11-25 96 views
1

我想從網頁中檢索數據,然後將其顯示在我的網頁上,沒有什麼花哨的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); 
        } 
       } 
       } 
+0

您的JavaScript需要在''標籤。 – Bardicer

+0

這是格式化在這裏嗎?如果是這樣的提示,我會記住,如果它關於實際的代碼,我有他們在不同的文件,並引用JS使用 CallumRob

+0

你有什麼嘗試,doesn不工作?你知道如何使用Javascript將元素插入DOM嗎?或者你沒有從Ajax調用中獲取數據?如果你能夠使用它,JQuery可能有助於簡化你的代碼。 – Phil

回答

-1

我不知道你的情況,但相同的起源策略限制通過XMLHttpRequest檢索到的內容從不同原點的網站訪問。去檢查這StackExchange answer

+0

您是否認爲我會爲此使用一些PHP,然後使用我的AJAX與PHP進行通信? – CallumRob

+0

爲什麼downvote?我認爲這是一個非常有效的案例,因爲OP不包括HTTP響應的頭部,或者來自控制檯的任何錯誤報告等。錯誤可能不在應用程序中,而是由於此限制而出現在請求本身中 – ebudi

+0

你的建議是正確的,我不知道你爲什麼得到了一個投票,因爲我使用了它已經開始工作的PHP腳本。謝謝 – CallumRob