我想從api中使用JSON來生成隨機引號。點擊按鈕,json應該填充「消息將會到達這裏」的位置。但我發現卡住了。代碼如下表和鏈接項目:使用jQuery getJSON方法獲取JSON
https://codepen.io/monsieurshiva/pen/awBbEE
<html>
<head>
<script>
$(document).ready(function() {
$("#getMessage").on("click", function(){
$.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en", function(json) {
$(".message").html(JSON.stringify(json));
});
});
});
</script>
</head>
<body>
<div class = "col-xs-12 well message">
The message will go here
</div>
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</body>
</html>
我認爲它因爲跨域錯誤的? – AdhershMNair
你應該閱讀你的json對象的引用,然後在你的div上添加引用來檢查這個[tutorial](https://www.w3schools.com/js/js_json_objects.asp)閱讀json對象,再加上確保你[允許](https://stackoverflow.com/questions/6871021/how-to-enable-cross-domain-request-on-the-server)跨域ajax調用 – Munzer
https://j11y.io/javascript/cross -domain-請求與 - jQuery的/ – Freak