我想要做的是通过REST API实现搜索功能。为此我选择了一个单独的搜索结果集合。
这就是我所拥有的:
收藏:
var mira = mira || {};
mira.IndustrySearchCollection = Backbone.Collection.extend({
model: mira.Industry,
initialize: function(models, options) {
this.query = options.query;
},
url: function() {
return "http://localhost:8000/manageDb/mira/industries/?q=" + this.query;
}
});
型号:
var mira = mira || {};
mira.Industry = Backbone.Model.extend({});
以下是我尝试执行搜索的方式:
var result = new mira.IndustrySearchCollection([], {query: "Ag"});
result.fetch({
error: function(){
console.log("Something went wrong");
},
success: function(){
console.log("It werks")
}});
问题是它总是返回错误。我期望它做的是在集合中有多个模型,并自动将服务器返回的属性绑定到它们的实际值。
以下是服务器返回的实际数据:
[{"id":2,"name":"Agriculture, forestry, fishing, and hunting","code":"11","id_classification":{"name":"CAN_NAICS","id_country":"Canada"}},
{"id":3,"name":"Agriculture, forestry, fishing, and hunting","code":"11","id_classification":{"name":"MEX_NAICS","id_country":"Mexico"}},
{"id":4,"name":"Agriculture, forestry, fishing, and hunting","code":"11","id_classification":{"name":"CHN_NAICS","id_country":"China"}},
{"id":231,"name":"Agricultural implement manufacturing","code":"33311","id_classification":{"name":"JP_NAICS","id_country":"Japan"}}]
编辑:更多信息:
它在collection.parse(...)
之前发生了一些问题,因为我试图覆盖它并检查响应但是它失败并出现同样的问题。
答案 0 :(得分:0)
好的。我很遗憾浪费一些人和他们。时间但问题与服务器有关。具体来说,这是一个CORS问题,因为我的服务器在不同的端口上运行(我认为不同的端口仍然被认为来自同一个来源)。