我看不出有什么问题。
我正在尝试在不同的服务器上获取数据,集合中的url是正确的,但返回404错误。尝试获取数据时,会触发错误功能,并且不会返回任何数据。返回数据的php脚本工作,并按预期给我输出。任何人都可以看到我的代码出了什么问题吗?
提前致谢:)
// function within view to fetch data
fetchData: function()
{
console.log('fetchData')
// Assign scope.
var $this = this;
// Set the colletion.
this.collection = new BookmarkCollection();
console.log(this.collection)
// Call server to get data.
this.collection.fetch(
{
cache: false,
success: function(collection, response)
{
console.log(collection)
// If there are no errors.
if (!collection.errors)
{
// Set JSON of collection to global variable.
app.userBookmarks = collection.toJSON();
// $this.loaded=true;
// Call function to render view.
$this.render();
}
// END if.
},
error: function(collection, response)
{
console.log('fetchData error')
console.log(collection)
console.log(response)
}
});
},
// end of function
模型和集合:
BookmarkModel = Backbone.Model.extend(
{
idAttribute: 'lineNavRef'
});
BookmarkCollection = Backbone.Collection.extend(
{
model: BookmarkModel,
//urlRoot: 'data/getBookmarks.php',
urlRoot: 'http://' + app.Domain + ':' + app.serverPort + '/data/getBookmarks.php?fromCrm=true',
url: function()
{
console.log(this.urlRoot)
return this.urlRoot;
},
parse: function (data, xhr)
{
console.log(data)
// Default error status.
this.errors = false;
if (data.responseCode < 1 || data.errorCode < 1)
{
this.errors = true;
}
return data;
}
});
答案 0 :(得分:0)
您可以使用JSONP发出请求(请参阅此处:http://en.wikipedia.org/wiki/JSONP)。
要使用Backbone实现它,只需执行以下操作:
var collection = new MyCollection();
collection.fetch({ dataType: 'jsonp' });
你的后端必须准备好这样做。服务器将接收由jQuery生成的回调名称,并在查询字符串上传递。所以服务器必须回复:
name_of_callback_fuction_generated({ YOUR DATA HERE });
希望我能帮到你。
答案 1 :(得分:-1)
这是一个跨域请求 - 没有办法。需要使用本地脚本并使用curl访问另一个域中的那个。