使用主干.js在不同的服务器上获取数据

Fetch data on different server with backbone.js

本文关键字:服务器 获取 数据 js      更新时间:2023-09-26

我看不出这有什么问题。

我正在尝试在其他服务器上获取数据,集合中的 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 访问另一个域上的脚本。