Javascript 在.getJson()中传递同一参数的多个值
我正在尝试以下内容:Javascript 在.getJson()中传递同一参数的多个值,javascript,jquery,json,api,youtube,Javascript,Jquery,Json,Api,Youtube,我正在尝试以下内容: <script> var topic_id = '/m/04136kj'; var service_url = 'https://www.googleapis.com/freebase/v1/topic'; var params = { filter: "/common/topic/article", filter:"/common/topic/notable_for",
<script>
var topic_id = '/m/04136kj';
var service_url = 'https://www.googleapis.com/freebase/v1/topic';
var params = {
filter: "/common/topic/article",
filter:"/common/topic/notable_for",
filter: "/common/topic/notable_types",
filter: "/type/object/type"
};
$.getJSON(service_url + topic_id + '?callback=?', params, function(topic) {
//do something with response.
});
</script>
将筛选器对象创建为数组
var topic_id = '/m/04136kj';
var service_url = 'https://www.googleapis.com/freebase/v1/topic';
var params = {
filter: ["/common/topic/article", "/common/topic/notable_for", "/common/topic/notable_types", "/type/object/type"]
};
//used to remove the the suffix [] in the array param
$.ajaxSetup({ traditional: true });
$.getJSON(service_url + topic_id + '?callback=?', params, function (topic) {
//do something with response.
});
param对象需要像HTML
对象一样创建,作为一系列名称/值对
var topic_id = '/m/04136kj';
var service_url = 'https://www.googleapis.com/freebase/v1/topic';
var filter = ["/common/topic/article", "/common/topic/notable_for",
"/common/topic/notable_types", "/type/object/type"];
var params = filter.map(function(f) { return { name: "filter", value: f }; });
$.getJSON(service_url + topic_id + '?callback=?', params, function (topic) {
//do something with response.
});
谢谢它起作用了,我理解其中的逻辑。我真是太蠢了:做了个小问题。Url的编码如下:
https://www.googleapis.com/freebase/v1/topic/m/04136kj?callback=jsonp1403174148707&filter[]=/common/topic/article
我注意到过滤器后面有一个[]
。你知道为什么吗?我和费德勒确认了。它正在通过过滤器传递[]
,这会导致错误响应。有什么想法吗?因为嵌入了[]
var topic_id = '/m/04136kj';
var service_url = 'https://www.googleapis.com/freebase/v1/topic';
var filter = ["/common/topic/article", "/common/topic/notable_for",
"/common/topic/notable_types", "/type/object/type"];
var params = filter.map(function(f) { return { name: "filter", value: f }; });
$.getJSON(service_url + topic_id + '?callback=?', params, function (topic) {
//do something with response.
});