JSONP回调的问题 [英] jsonp callback problem
本文介绍了JSONP回调的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试着以下code,以获取客户端的IP,并能正常工作
<脚本类型=文/ JavaScript的>
功能getip(JSON){
VAR IP = json.ip; //提醒客户端IP地址
警报(IP);
}
< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =http://jsonip.appspot.com/?callback=getip>< / SCRIPT>
但是当我与 $。阿贾克斯
尝试它什么也不做......
$。阿贾克斯({
键入:GET,
网址:http://jsonip.appspot.com/?callback=getip,
数据类型:JSONP
成功:函数getip(JSON){
警报(sucess);
VAR IP = json.ip;
警报(IP);
}
});
});
plz帮助
解决方案
$。阿贾克斯({
键入:GET,
网址:http://jsonip.appspot.com/?callback=?,
// ^
// ----注意的?符号---------------- |
// jQuery是负责更换这个符号
//与自动生成的回调FN的名字
数据类型:JSONP
成功:函数(JSON){
VAR IP = json.ip;
警报(IP);
}
});
的jsfiddle演示这里。
i'm trying the following code to retrieve the client ip, and it works fine
<script type="text/javascript">
function getip(json) {
var ip = json.ip; // alerts the client ip address
alert(ip);
}
</script>
<script type="text/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>
but when i try it with $.ajax
it does nothing...
$.ajax({
type: "GET",
url: 'http://jsonip.appspot.com/?callback=getip',
dataType: "jsonp",
success: function getip(json) {
alert("sucess");
var ip = json.ip;
alert(ip);
}
});
});
plz help
解决方案
$.ajax({
type: "GET",
url: "http://jsonip.appspot.com/?callback=?",
// ^
// ---- note the ? symbol ----------------|
// jQuery is responsible for replacing this symbol
// with the name of the auto generated callback fn
dataType: "jsonp",
success: function(json) {
var ip = json.ip;
alert(ip);
}
});
jsFiddle demo here.
这篇关于JSONP回调的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文