dspym82000
2015-11-26 01:18
浏览 72
已采纳

backbone.js model.fetch没有向php发送数据

I'm just starting to play around with backbone.js to learn more about it for a project where I use Javascript for client side code an PHP on the server side.

To find out how communications between backbone and the server actually works (can't really find any good documentation about it. I just made a small example for fetch():

var NavigationElement = Backbone.Model.extend({
    initialize: function() {},
    defaults:   {
                    text: 'Home',
                    link: '/',
                    category: 'Main'
                },
    url: 'ajax.php'

});
var forum = new NavigationElement( { text: 'Forum', 
                                     link: '?q=Forum'});
var login = new NavigationElement( { text: 'Login', 
                                     link: '?q=Login', 
                                     category: 'User'});

forum.fetch();

Now, Backbone should send some GET data to the server to identify the data it has to send. And indeed, $_SERVER[ 'REQUEST_METHOD' ] says there is a GET-Request, but $_GET remains empty.

For save() I had a similar problem and learned, that I could find the data in php://input for all POST and PUT Requests. Is there a similar stream for GET Requests?

And how can I tell Backbone to send a GET-Request like '.../ajax.php?model=NavigationElement&text=Login' ?

图片转代码服务由CSDN问答提供 功能建议

我刚刚开始使用backbone.js来了解更多关于我使用Javascript的项目的信息 对于客户端代码,在服务器端使用PHP。

了解主干和服务器之间的通信是如何工作的(无法真正找到有关它的任何好文档。我只是做了一个 fetch()的小例子:

  var NavigationElement = Backbone.Model.extend({
 initialize:function(){},
 默认值:{
 text:'Home',
 link:'/',
 category:'Main'
},
 url:'ajax.php'
 
}); 
var forum =  new NavigationElement({text:'Forum',
 link:'?q = Forum'}); 
var login = new NavigationElement({text:'Login',
 link:'?q = Login',
 类别:'用户'}); 
 
forum.fetch(); 
   
 
 

现在,Bac kbone应该向服务器发送一些 GET 数据,以识别它必须发​​送的数据。 事实上, $ _ SERVER ['REQUEST_METHOD'] 表示存在GET-Request,但 $ _ GET 仍为空。

For save()我有类似的问题并且学到了,我可以在 php:// input 中找到所有 POST PUT 请求。 是否有类似的 GET 请求流?

如何告诉Backbone发送GET-Request,如'... / ajax。 php?model = NavigationElement& text = Login'?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanqiangwu9332 2015-11-26 10:02
    已采纳

    By default, model.fetch will use the first available in

    • the url attribute passed in the options to fetch
    • what is set by model.url
    • model.urlRoot with /id appended if the id attribute exists
    • model.collection.url with /id appended if the id attribute exists

    You can provide a custom url method on NavigationElement to set a different endpoint. For example:

    var NavigationElement = Backbone.Model.extend({
        initialize: function() {},
        defaults: {
            text: 'Home',
            link: '/',
            category: 'Main'
        },
        url: function() {
            return 'ajax.php?model=NavigationElement&text='+encodeURIComponent(this.get('text'));
        }
    });
    

    A demo : http://jsfiddle.net/nikoshr/ygdbxn1q/

    or if you just want to pass additional parameters on read operations, you can override fetch and pass the data as an option:

    var NavigationElement = Backbone.Model.extend({
        initialize: function() {},
        defaults: {
            text: 'Home',
            link: '/',
            category: 'Main'
        },
        url: 'ajax.php',
        fetch: function(options) {
            options = options || {};
            options.data = _.extend({
                model: 'NavigationElement'
            }, this.pick('text'));
            return Backbone.Model.prototype.fetch.call(this, options);
        }
    });
    

    http://jsfiddle.net/nikoshr/ygdbxn1q/1/

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题