2

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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

0

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.

2 Comments

Try to include the content of the link in your answer. All external links are not available to all users.
I dont wanna write all the things here. I am lazy for it.I just want to share the solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.