使用主干.js在不同的服务器上获取数据
Fetch data on different server with backbone.js
我看不出这有什么问题。
我正在尝试在其他服务器上获取数据,集合中的 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;
}
});
您可以使用
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 });
希望我有所帮助。
这是一个跨域请求 - 不行。 需要使用本地脚本并使用 curl 访问另一个域上的脚本。
相关文章:
- 从服务器获取数据并在选择控件中使用ng选项无法显示选项
- 事件循环的MEAN.JS setInterval进程(从另一个服务器获取数据)
- 使用ReactJS和Flux架构从服务器获取数据的正确方法是什么
- 我在哪里从 React Redux 应用程序中的服务器获取初始数据
- 从服务器获取并传递给浏览器以在 Javascript 中格式化的日期时间格式
- Fetch Api无法从PHP服务器获取会话
- 如何从Python服务器获取到Javascript客户端
- jQueryUI FullCalendar:从服务器获取事件后的回调
- 从服务器获取数据,并使用$http服务和ng-change在select事件上显示它
- 无法从服务器获取json数据
- 将从服务器获取的数据加载到输入文本表单中
- javascript如何从服务器获取文件夹中文件的url
- 如何从外部服务器获取JSON
- angularjs设计模式和从服务器获取数据的最佳实践
- 使用Jquery和Ajax从服务器获取值的跨域Ajax请求
- 如何在从服务器获取数据的动态表的每一行中添加下拉列表
- 使用Angular从服务器获取数据
- 使用AngularJS从服务器获取计算结果
- AngularJS web客户端可以't从Scala Play服务器获取资源
- 在我的谷歌自定义搜索引擎从服务器获取数据后,我如何激发一些javascript代码