Rafay
Rafay

Reputation: 31033

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

Upvotes: 2

Views: 4841

Answers (3)

vitralyoz
vitralyoz

Reputation: 618

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.

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272036

$.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.

Upvotes: 4

billygoat
billygoat

Reputation: 21984

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.?

Upvotes: 3

Related Questions