[問題]怎麽自動更新來自另一個網址的JSON
我想做兩件事
1.取出網頁內容JSON,拆到元件
2.傳給另一個網頁呈現,
所以想一起問
JS跟AJAX都很菜,只有google的程度,不好意思
我寫了個JSP會由網址輸入不同參數改變輸出的JSON,
網址為
140.138.77.151:8080/personalAssementNEW
會輸出以下網頁
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
[{"UID":999999,"UrlDate":"2015-11-24","SleepQ":70,"SleepQ_Lab":"普通
","AttLevel":43.2864439246683,"AttLevel_Lab":"普通
","MemLevel":60,"MemLevel_Lab":"普通"}]
<h1></h1>
</body>
</html>
想取出其中這段JSON的元件,呈現至另一個網頁上
[{"UID":999999,"UrlDate":"2015-11-24","SleepQ":70,"SleepQ_Lab":"普通
","AttLevel":43.2864439246683,"AttLevel_Lab":"普通
","MemLevel":60,"MemLevel_Lab":"普通"}]
我找了w3c範例套用看看但沒有東西,如以下:
$(document).ready(function(){
$("button").click(function(){
$.get("140.138.77.151:8080/personalAssementNEW/", function(data,
status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
發現需要"Access-Control-Allow-Origin",修改後另一個試著寫的JS可以讀出網頁結果
但是AJAX還是不能解元件:
<!DOCTYPE html>
<html>
<body>
<div id="id01"></div>
<script>
var xmlhttp = new XMLHttpRequest();
/*var url = "http://www.w3schools.com/website/Customers_MYSQL.php";*/
var url;
url =
"http://140.138.77.151:8080/personalAssementNEW/getAssement.jsp?ID=999999&DATEINFO=2015-11-24&TIMEINFO=15:00:00";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
document.getElementById("id01").innerHTML = response;
var obj = JSON.parse(response);
document.getElementById("id01").innerHTML = obj[0].AttLevel + " " +
obj[0].AttLevel_Lab;
}
</script>
</body>
</html>
他只會輸出[{.....}],我後面的 obj[0].AttLevel 就不理我了。之前用靜態網頁是OK的
所以想請教怎麽取得JSON(應該要用AJAX?)自動隨著網址的內容更新
呈現在另一個網頁上
如果有很小白的部分請多包容,謝謝
問題同時於以下詢問,但是可能太菜沒人理我XDrz
https://stackoverflow.com/questions/40856472/how-can-i-auto-update-a-web-pages-json-content-by-ajax
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.138.148.133
※ 文章網址: https://www.ptt.cc/bbs/Ajax/M.1480430148.A.D2E.html
推
11/30 03:11, , 1F
11/30 03:11, 1F
推
11/30 03:13, , 2F
11/30 03:13, 2F
→
11/30 03:29, , 3F
11/30 03:29, 3F
→
11/30 03:32, , 4F
11/30 03:32, 4F
→
11/30 03:54, , 5F
11/30 03:54, 5F
→
11/30 03:54, , 6F
11/30 03:54, 6F
→
11/30 03:56, , 7F
11/30 03:56, 7F
→
11/30 03:56, , 8F
11/30 03:56, 8F
推
11/30 09:12, , 9F
11/30 09:12, 9F
推
11/30 09:26, , 10F
11/30 09:26, 10F
→
11/30 09:26, , 11F
11/30 09:26, 11F
→
11/30 09:27, , 12F
11/30 09:27, 12F
→
11/30 09:27, , 13F
11/30 09:27, 13F
→
11/30 09:28, , 14F
11/30 09:28, 14F
→
11/30 14:12, , 15F
11/30 14:12, 15F
→
11/30 14:12, , 16F
11/30 14:12, 16F
→
11/30 14:13, , 17F
11/30 14:13, 17F
→
11/30 14:15, , 18F
11/30 14:15, 18F
→
11/30 14:28, , 19F
11/30 14:28, 19F
→
11/30 14:28, , 20F
11/30 14:28, 20F
→
11/30 15:41, , 21F
11/30 15:41, 21F
推
11/30 17:08, , 22F
11/30 17:08, 22F
→
11/30 18:15, , 23F
11/30 18:15, 23F
推
11/30 20:04, , 24F
11/30 20:04, 24F
→
11/30 20:47, , 25F
11/30 20:47, 25F
→
11/30 20:47, , 26F
11/30 20:47, 26F
推
12/01 14:51, , 27F
12/01 14:51, 27F
→
12/01 16:54, , 28F
12/01 16:54, 28F
→
12/01 16:54, , 29F
12/01 16:54, 29F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 3 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章