I’m just messing around with the Factual API but I don’t fully understand how to represent a URL as a get request. The following is the URL I want to turn into a jQuery get:
http://api.factual.com/v2/tables/7HOTBC/read?APIKey=SoX1rlj4x8VfRfvqnnehVH5ObpkHJc0kIzloTtxor5gyrHG5c3EySCTTcErCyRYO&filters={"category":{"$bw":"Arts%2C%20Entertainment%20%26%20Nightlife%20%3E%20Bars"},"latitude":{"$blank":false},"longitude":{"$blank":false},"$search":["London"],"$loc":{"$within_dist":[51.5149943,-0.0638818,1000]}}
This is the code I have written to do this:
var factualKey = "SoX1rlj4x8VfRfvqnnehVH5ObpkHJc0kIzloTtxor5gyrHG5c3EySCTTcErCyRYO";
$(document).ready(function(){
// initialise google maps
initializeGoogleMaps();
// make a get request to the factual api
$.get(
"http://api.factual.com/v2/tables/7HOTBC/read",
{ APIKey : factualKey,
filters : { category : { $bw : "Arts, Entertainment & Nightlife > Bars" },
latitude : { $blank : false },
longitude : { $blank : false },
$search : "[\"London\"]",
$loc : { $within_dist : [51.5149943,-0.0638818,1000] }
}
},
function(responseData){
alert("SUCCESS");
},
"json"
);
});
When I look in firebug this is what the address of my request has come out like:
http://api.factual.com/v2/tables/7HOTBC/read?APIKey=SoX1rlj4x8VfRfvqnnehVH5ObpkHJc0kIzloTtxor5gyrHG5c3EySCTTcErCyRYO&filters[category][%24bw]=Arts%2C+Entertainment+%26+Nightlife+%3E+Bars&filters[latitude][%24blank]=false&filters[longitude][%24blank]=false&filters[%24search]=[%22London%22]&filters[%24loc][%24within_dist][]=51.5149943&filters[%24loc][%24within_dist][]=-0.0638818&filters[%24loc][%24within_dist][]=1000
…which is obviously wrong!
And here’s the response from the Factual API:
{"version":"2","status":"error","error":"Your filters parameter cannot be parsed. Please see http:\/\/wiki.developer.factual.com\/Server-API for documentation.","error_type":"Api::Error::InvalidArgument"}
Can someone tell me what I haven’t done properly in my code? Am I meant to encode the data I’m passing as part of the get request? Or is it something I’ve missed?
Here’s how I got a successful querystring using your object.
Same as what you’re doing, except that I
JSON.stringifythe filters, and$.paramthe entire thing.Not sure if the
$.paramis needed. Could be that jQuery does it.Also not sure if you’ll hit same origin policy issues.