0

I'm trying to do a get request to a website and get the response.

I'm able to do a successful HTTP request and response, but for some reason the javascript error pops up and it breaks my code.

I'm assuming it is because of the new query parameter it is appending, like http://checkip.amazonaws.com/?callback=jQuery152011860558553598821_1374187260201&_=1374187260214

Code here:

http://jsfiddle.net/R7EPt/191/

$('document').ready(function() {
    var url = 'http://checkip.amazonaws.com/';
    $.getJSON(url + "?callback=?", null, function(data) {
        alert(data);
    });
 });

Could some one help me!

Adi GuN
  • 1,244
  • 3
  • 16
  • 38
  • No, that's just a parameter added by jQuery to prevent the browser caching the request you just done – Niccolò Campolungo Jul 18 '13 at 22:51
  • So what do you think the error would be? It points to this link and says Uncaught SyntaxError: Unexpected number. You could give it a try http://jsfiddle.net/R7EPt/191/ – Adi GuN Jul 18 '13 at 22:53
  • I think the problem is the retrieved content itself, it is not valid JSON, you should do the request with a simple `$.get`, see [here](http://api.jquery.com/jQuery.get/) – Niccolò Campolungo Jul 18 '13 at 22:53
  • But with simple get it throws be a Cross Origin error. – Adi GuN Jul 18 '13 at 22:55
  • I don't know how to solve this, but I have a sidenote for you: it's `$(document)`, not `$('document')`, since `document` is an object and not an HTML tag – Niccolò Campolungo Jul 18 '13 at 23:02

1 Answers1

1

You are trying to issue a JSONP request from the client, but the server does not return a JSONP response. A JSONP response for the request checkip.amazonaws.com/?callback=foo would look like this:

foo("192.168.0.1");

Instead of just this:

192.168.0.1

Edit

A solution to receive the user's IP via JSONP: Get user ip with jquery

Community
  • 1
  • 1
Rotem Harel
  • 736
  • 8
  • 16