Javascript 将 getJSON 更改为 JSONP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11916780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:38:54 来源:igfitidea点击:
Changing getJSON to JSONP
提问by Satch3000
I have this code:
我有这个代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.getJSON('http://example.com/api/get_cats', function(fbResults) {
document.write(fbResults.cats[0].title);
});
});
</script>
How can I change this code:
如何更改此代码:
<script>
$(document).ready(function() {
$.getJSON('http://example.com/api/get_cats', function(fbResults) {
document.write(fbResults.cats[0].title);
});
});
</script>
for it to work as JSONP ... Is this totally different?
让它作为 JSONP 工作......这完全不同吗?
回答by Jill-Jênn Vie
Actually, you just have to add ?callback=?
, jQuery does the rest.
实际上,您只需要添加?callback=?
,其余的由 jQuery 完成。
$(document).ready(function() {
$.getJSON('http://example.com/api/get_cats?callback=?', function(fbResults) {
document.write(fbResults.cats[0].title);
});
});