【发布时间】:2011-12-06 23:24:14
【问题描述】:
我只是在搞乱 Factual API,但我不完全了解如何将 URL 表示为获取请求。以下是我要转成jQuery get的URL:
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]}}
这是我为此编写的代码:
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"
);
});
当我查看萤火虫时,我的请求地址是这样的:
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
...这显然是错误的!
这是来自 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"}
谁能告诉我我的代码中哪些地方没有正确完成?我是否打算对作为 get 请求的一部分传递的数据进行编码?还是我错过了什么?
【问题讨论】:
-
尝试通过
$.param()发送您的数据。$.param({APIKey:factualKey, filt...}) -
...没关系,当我这样做时,我会遇到与您相同的错误。
-
我认为您需要将您的过滤器 : 转换为 JSON 字符串,然后对其进行 urlencode。现在看起来 jQuery 正在尝试使用方括号来保留对象 --> filter[category][$bw]=etc... 而您真正想要的是 --> &filters={ url 编码的 json 数据没有被劫持通过 jQuery 参数化 (sp?) }