sca*_*joe 4 javascript api jquery json getjson
我正在尝试在Codepen中构建一个简单的随机引用生成器,如下所示:http://codepen.io/scabbyjoe/pen/dXQmLw
粘贴的相关代码如下:
<html>
<head>
</head>
<body>
<h1>Random Quote Machine</h1>
<div class="quote">
</div>
<div class="btn">New Quote</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function() {
$(".btn").on("click", function(){
$.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en", function(json) {
$(".quote").html(JSON.stringify(json));
});
});
});
Run Code Online (Sandbox Code Playgroud)
我担心我必须误解getJSON工具,因为我无法在我的.quotediv中加载任何内容.
任何人都可以解释为什么这不起作用?
我试着从这个(单独的)例子中得到遵循,这个例子确实有效:
<script>
$(document).ready(function() {
$("#getMessage").on("click", function(){
$.getJSON("/json/cats.json", function(json) {
$(".message").html(JSON.stringify(json));
});
});
});
</script>
<div class="container-fluid">
<div class = "row text-center">
<h2>Cat Photo Finder</h2>
</div>
<div class = "row text-center">
<div class = "col-xs-12 well message">
The message will go here
</div>
</div>
<div class = "row text-center">
<div class = "col-xs-12">
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
检查控制台是否有错误:
XMLHttpRequest无法加载http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http://s.codepen.io "访问.
你不能让AJAX请求托管在从自己的独立域名服务,除非他们特别允许.此消息表明不允许.
也许在某些情况下允许它; 检查他们的文件.如果不是,您将必须通过您自己的服务器代理请求.在这种情况下也不能保证它是允许的.您可能必须提供类似API密钥的内容或添加到白名单中.
| 归档时间: |
|
| 查看次数: |
1133 次 |
| 最近记录: |