开发者

jsonp callback problem

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.


Did you see the url that is passed across the wire? I suggest you try { jsonp: false, jsonpCallback: "callbackName" }. This will avoid jquery from adding the callback function automatically.

Also did you set cross domain to true.?


You dont need to add any callback parameter in url.

If you try http://terrasus.com/detail.jsp?articleID=396 article step by step it will work fine. if you produce jsonp response you should get the callback value and set it to your response dynamically. This article has a detail explanation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜