GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
GET request becoming OPTIONS request  XML
Forum Index -> FAQ - frequently asked questions
Author Message
kmarrowo



Joined: 06/08/2012 23:04:13
Messages: 2
Offline

Hello all,

I'm new to GeoNames, so apologies if this question has been asked before. I ran a forum search and didn't see anything.

I am trying to do a reverse zipcode lookup via Javascript in a JSP script. I am using the JSON response website for the query, but the GET request is being turned into an OPTIONS request instead.

I understand this has something to do with protection from cross server scripting, but was under the impression that using the JSON version would solve this problem.

I also made sure to enable Web Services for my username.

Here's my code.
Code:
     var xhrArgs = {
 		url: "http://api.geonames.org/findNearbyPostalCodesJSON",
 		handleAs: "json",
 		load: function(data) { 
             alert(dojo.toJson(data));
 		},
 		error: function(error) {
 			console.error(error);
 		},
 		content: {lat: lat, lng: lon, username: "kmarrowo"}
 	};
     
     dojo.xhrGet(xhrArgs);
 


The generated HTML string is correct:
Code:
http://api.geonames.org/findNearbyPostalCodesJSON?lat=25.78440&lng=-80.24348&username=kmarrowo


Unfortunately, the only request I see going out (via Firebug) is an OPTIONS request the above URL.

Any thoughts?
kmarrowo



Joined: 06/08/2012 23:04:13
Messages: 2
Offline

I found the solution.

It looks like by default dojo adds an 'X-Requested-With' argument to the Access-Control-Request-Header property, which typically causes servers to reject the request because it causes cross-site XHR requests to force preflight requests.

Here's the dojo ticket and bugfix info.:
Ticket
Bug Fix

Code:
 var xhrArgs = {
 		url: "http://api.geonames.org/findNearbyPostalCodesJSON",
 		handleAs: "json",
 		headers: {                     // This is required to stop the
 			"X-Requested-With": "" // server from rejecting the 
 		},			       // GET request
 		load: function(data) {
 			// Do things
 		},
 		error: function(error) {
 			console.error(error);
 		},
 		content: {lat: lat, lng: lon, maxRows: "1", style: "medium", country: "US", username: "kmarrowo"}
 	};
     
 dojo.xhrGet(xhrArgs);
 
 
Forum Index -> FAQ - frequently asked questions
Go to:   
Powered by JForum 2.1.5 © JForum Team