dspym82000 2015-11-26 01:18 采纳率: 100%
浏览 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' ?

  • 写回答

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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错