2014-04-08 20 views
0

我看不到这是什么问题。使用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; 
    }  
}); 

回答

-1

这是一个跨域请求 - 没有做不到的。将需要使用本地脚本并使用curl访问其他域上的脚本。

+0

尽管事实上你不能用AJAX调用进行跨域请求,但并不意味着它不能通过其他技术来完成。你可以使用JSONP。 –

0

您可以使用JSONP进行请求(请阅读此处:http://en.wikipedia.org/wiki/JSONP)。

为了更好地实现它使用骨干,只需做到这一点:

var collection = new MyCollection(); 

collection.fetch({ dataType: 'jsonp' }); 

您后端必须准备好这样做。服务器将收到由jQuery生成的回调名称,并传递给查询字符串。所以服务器必须回应:

name_of_callback_fuction_generated({ YOUR DATA HERE }); 

希望我帮了忙。

相关问题