【发布时间】:2016-02-26 23:57:50
【问题描述】:
我无法触发 getjson 成功事件。当我在 $(document).ready 上调用 $.getJSON 时,它工作正常,当我将相同的代码放在按钮单击下时,它就不起作用了。
工作正常(在 $(document).ready 下)
<html>
<head>
<title>API Logger</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js" ></script>
<script>
"use strict";
$(document).ready(function(){
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON( flickerAPI,
{
tags: "mount everest",
tagmode: "any",
format: "json"
},
function(data)
{
alert("success");
});
});
</script>
</head>
<body>
<form>
<button id="btn1" >Execute</button>
</form>
</body>
不工作(在 $('#btn1').on('click', function()
<html>
<head>
<title>API Logger</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js" ></script>
<script>
"use strict";
$(document).ready(function(){
$('#btn1').on('click', function() {
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON( flickerAPI,
{
tags: "mount everest",
tagmode: "any",
format: "json"
},
function(data)
{
alert("success");
});
});
});
</script>
</head>
<body>
<form>
<button id="btn1" >Execute</button>
</form>
</body>
【问题讨论】:
标签: javascript jquery html jsonp getjson