JQuery JSON query gets a “200 OK” response but no content

I use jQuery to get the visitor’s location through their IP address. There's a great service for this called freeGeoIP . All I need to do is "json" or "xml" at the end of their URL, then add the IP address and return the required data.

When I do this manually in my browser, it works: I get a tiny document to download. When I make a request for $ .ajax or $ .getJSON from a browser, it responds with a “200 OK” heading and metadata below. But the actual data is not coming. What's happening?

EDIT: I added javascript / jQuery code:

function openForm(event,ui){ var _this = $(this); //Get details on the user IP var myIP = $('#yourIP').attr('ip');alert(myIP); var url = 'http://freegeoip.appspot.com/json/' + myIP; $.ajax({ url: url, dataType: 'json', contentType: 'text/json', timeout: 10000, complete: function(ip){ alert('Success Ajax!'); //URL returns status,ip,countrycode,countryname,regioncode,regionname,city,zipcode,latitude,longitude $('#yourIP').text(ip.city + ", " + ip.countryname + " at " + ip.latitude + " latitude."); $('#yourIP').attr({'city': ip.city,'country': ip.countryname}); } }); RESPONSE HEADERS Cache-Control no-cache Content-Type text/json Expires Fri, 01 Jan 1990 00:00:00 GMT Content-Encoding gzip Date Fri, 17 Dec 2010 15:26:48 GMT Server Google Frontend Content-Length 156 REQUEST HEADERS Host freegeoip.appspot.com User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729) Accept application/json, text/javascript, */*; q=0.01 Accept-Language nl,en-us;q=0.7,en;q=0.3 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 115 Connection keep-alive Content-Type text/json Referer http://www.freshbase.nl/permaculture/index.php Origin http://www.freshbase.nl 
+4
source share
5 answers

This is because you are restricting the cross-domain query of the XMLHttpRequest object used in jQuery AJAX functions. Some browsers throw an empty 200 response when this happens (which is confusing).

You need to either use a service that supports JSONP to circumvent cross-domain access, or use a server-side proxy in the same domain to act as a local broker.

+10
source

200 means that the request returns correctly without errors. This was a successful request.

A successful query cannot return content like an SQL query that does not return records.

+3
source

You are trying to execute an Ajax request for Cross Site. Browsers block such manipulations as being dangerous to security. See how to solve your problem: AJAX Cross-Site Requests

+2
source

all you need to add jsoncallback as a querystring parameter

 var url = 'http://freegeoip.appspot.com/json/' + myIP + "&jsoncallback=?; $.getJSON(url, function(data){ }); 
+1
source

So, I solved my problem! Thanks to everyone for pointing me to a few ways that helped me understand more about cross-site requests, JSONP, etc.

It is really very simple. A page that wants to have this data consists of PHP. So I added these two lines in PHP:

 $myIP = $_SERVER['REMOTE_ADDR']; $myGeoData = file_get_contents('http://freegeoip.appspot.com/json/' . $myIP); 

and then repeat these variables in the appropriate page bits for jQuery to pick up and act.

Thus, if the web service provides its data in the form of a file or a simple string, then all the files necessary to receive this data begin with file_get_contents.

You can get it in PHP either before sending the entire page to the browser, or you can make an Ajax request to a specially created PHP script on your own server that uses this command. Thanks again!

+1
source

Source: https://habr.com/ru/post/1332520/


All Articles