JSONP回调的问题回调、问题、JSONP

2023-09-10 17:06:03 作者:酒瘾

我试着以下code,以获取客户端的IP,并能正常工作

 <脚本类型=文/ JavaScript的>
    功能getip(JSON){
        VAR IP = json.ip; //提醒客户端IP地址
        警报(IP);
    }
< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =htt​​p://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.