<small id='E0mzO'></small><noframes id='E0mzO'>

<i id='E0mzO'><tr id='E0mzO'><dt id='E0mzO'><q id='E0mzO'><span id='E0mzO'><b id='E0mzO'><form id='E0mzO'><ins id='E0mzO'></ins><ul id='E0mzO'></ul><sub id='E0mzO'></sub></form><legend id='E0mzO'></legend><bdo id='E0mzO'><pre id='E0mzO'><center id='E0mzO'></center></pre></bdo></b><th id='E0mzO'></th></span></q></dt></tr></i><div id='E0mzO'><tfoot id='E0mzO'></tfoot><dl id='E0mzO'><fieldset id='E0mzO'></fieldset></dl></div>
    <bdo id='E0mzO'></bdo><ul id='E0mzO'></ul>
  • <tfoot id='E0mzO'></tfoot>

      1. <legend id='E0mzO'><style id='E0mzO'><dir id='E0mzO'><q id='E0mzO'></q></dir></style></legend>
      2. Backbone Sync 返回一个空的 $_POST 数组

        时间:2023-09-24
        <i id='CfTZY'><tr id='CfTZY'><dt id='CfTZY'><q id='CfTZY'><span id='CfTZY'><b id='CfTZY'><form id='CfTZY'><ins id='CfTZY'></ins><ul id='CfTZY'></ul><sub id='CfTZY'></sub></form><legend id='CfTZY'></legend><bdo id='CfTZY'><pre id='CfTZY'><center id='CfTZY'></center></pre></bdo></b><th id='CfTZY'></th></span></q></dt></tr></i><div id='CfTZY'><tfoot id='CfTZY'></tfoot><dl id='CfTZY'><fieldset id='CfTZY'></fieldset></dl></div>

        <legend id='CfTZY'><style id='CfTZY'><dir id='CfTZY'><q id='CfTZY'></q></dir></style></legend>

        • <bdo id='CfTZY'></bdo><ul id='CfTZY'></ul>

              <tbody id='CfTZY'></tbody>

                <tfoot id='CfTZY'></tfoot>
              1. <small id='CfTZY'></small><noframes id='CfTZY'>

                  本文介绍了Backbone Sync 返回一个空的 $_POST 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Backbone 和 Yii 框架开发我的第一个 RESTful 应用程序.我对 GET 方法没有任何问题,但我现在坚持使用 POST 方法来创建一个新元素.

                  I'm trying to do my first RESTful app with Backbone and Yii Framework. I had no problem with the GET methods but I'm stuck now with the POST method, to create a new element.

                  我在 Backbone 中有一个评论模型:

                  I have a Comment model in Backbone:

                  var commentModel = Backbone.Model.extend({
                  
                      urlRoot: "index.php/api/comments",
                      idAttribute: 'id',
                  
                      defaults: {
                          content: "Empty comment",
                          status: 1
                      }
                  });
                  

                  在我看来,我添加了一个函数来创建一个新的注释,传递来自相对形式的值:

                  In my view I add a function to create a new Comment passing the values from the relative form:

                  on_submit: function(e) {
                              var new_comment = new Comment({author_id: this.$('#author_text').val(), content: this.$('#content_text').val(), post_id: this.$("#post_text").val(), status: this.$("#status_text").val()});
                  
                           new_comment.save();
                          },
                  

                  使用 Firebug 查看请求似乎没问题,在 POST 选项卡中我可以看到所有值:

                  Looking the request with Firebug it seems all right, in the POST tab I can see all the values:

                  JSON            
                  author_id "7"   
                  content "Epic fail"
                  post_id "7" 
                  status "2"
                  
                  Source 
                  {"content":"Epic fail","status":"2","author_id":"7","post_id":"7"}
                  

                  但是在我的 php Api 中,$_POST 变量是空的!

                  But in my php Api the $_POST var is empty!

                  foreach($_POST as $var=>$value) {
                  
                       if($model->hasAttribute($var))
                          $model->$var = $value;
                       else
                          $this->_sendResponse(500);
                  }
                  

                  有人有什么想法吗?阅读 Backbone.Sync 的文档我明白它应该使用 POST 来创建请求.

                  Anyone has some ideas? Reading the documentation of Backbone.Sync I understand that it should use POST for create request.

                  我找到了一种从以下位置获取值的解决方法:

                  I found a workaround getting the values from:

                  file_get_contents('php://input') 
                  

                  但是我觉得id不合适...

                  but id doesn't feel right to me...

                  谢谢.

                  推荐答案

                  来自 Backbone.sync 文档,

                  使用默认实现,当 Backbone.sync 发送请求时保存模型,它的属性将被传递,序列化为 JSON,并在内容类型为 application/json 的 HTTP 正文中发送.

                  With the default implementation, when Backbone.sync sends up a request to save a model, its attributes will be passed, serialized as JSON, and sent in the HTTP body with content-type application/json.

                  这意味着您不会像在常规表单帖子中那样在参数中接收数据,而是在 HTTP 响应的正文中接收数据.

                  which means you won't receive data in parameters like in a regular form post but in the body of the HTTP response.

                  您有三种选择来处理这种行为:

                  You've got three options to deal with this behavior:

                  1. 修改您的服务器代码以了解 REST 请求,请参阅插入Backbone.js 模型导入 MySQL 数据库 例如
                  2. 看看 Backbone.emulateJSON 能否满足您的需求
                  3. 覆盖 Backbone.sync 并发送数据以更好地适应您的服务器
                  1. Modify your server code to understand REST requests, see Insert Backbone.js model into MySQL database for example
                  2. See if Backbone.emulateJSON could answer your needs
                  3. Overwrite Backbone.sync and send the data to better fit your server

                  这篇关于Backbone Sync 返回一个空的 $_POST 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Yii CDbCriteria 加入 下一篇:PHP 中的命名空间阻止使用 Yii 静态函数

                  相关文章

                  <small id='j1dmp'></small><noframes id='j1dmp'>

                • <i id='j1dmp'><tr id='j1dmp'><dt id='j1dmp'><q id='j1dmp'><span id='j1dmp'><b id='j1dmp'><form id='j1dmp'><ins id='j1dmp'></ins><ul id='j1dmp'></ul><sub id='j1dmp'></sub></form><legend id='j1dmp'></legend><bdo id='j1dmp'><pre id='j1dmp'><center id='j1dmp'></center></pre></bdo></b><th id='j1dmp'></th></span></q></dt></tr></i><div id='j1dmp'><tfoot id='j1dmp'></tfoot><dl id='j1dmp'><fieldset id='j1dmp'></fieldset></dl></div>
                  • <bdo id='j1dmp'></bdo><ul id='j1dmp'></ul>
                  <tfoot id='j1dmp'></tfoot>
                  <legend id='j1dmp'><style id='j1dmp'><dir id='j1dmp'><q id='j1dmp'></q></dir></style></legend>