【问题标题】:Cannot get body from DELETE request with Slim Framework无法使用 Slim 框架从 DELETE 请求中获取正文
【发布时间】:2015-08-19 04:48:57
【问题描述】:

我正在使用backbone.js 和Slim 框架开发一个应用程序。 问题就在这里:我需要通过执行一个 MySQL 存储过程来删除一条记录,该存储过程除了要删除的记录的 ID 之外,还接收另一个参数来执行一些业务逻辑。 问题是我只收到记录的 ID。我的 $app->request()->getBody();为空白。 代码如下:

/rest/folder/index.php

<?php
require_once '../../vendors/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app -> get('/categories/', function() use ($app) {
    require_once ('../../libs/auth.inc');
    $auth = new Authentication();
    $user_id = $auth -> SessionAuthenticate();

    require_once ('categories.php');
    $categories = new Categories();
    $categories -> getCategories($app, $user_id);
});

$app -> post('/categories/', function() use ($app) {
    require_once ('../../libs/auth.inc');
    $auth = new Authentication();
    $user_id = $auth -> SessionAuthenticate();

    require_once ('categories.php');
    $categories = new Categories();
    $categories -> saveCategory($app, $user_id);
});

$app -> put('/categories/:category_id', function($category_id) use ($app) {
    require_once ('../../libs/auth.inc');
    $auth = new Authentication();
    $user_id = $auth -> SessionAuthenticate();

    require_once ('categories.php');
    $categories = new Categories();
    $categories -> saveCategory($app, $user_id);
});

$app -> delete('/categories/:category_id', function ($category_id) use ($app) {
    require_once ('../../libs/auth.inc');
    $auth = new Authentication();
    $user_id = $auth -> SessionAuthenticate();

    require_once('categories.php');
    $categories = new Categories();
    $categories -> deleteCategory($app, $user_id, $category_id);
});

$app -> run();

这是我在 /rest/folder/categories.php 上的删除功能

public function deleteCategory($app, $user_id,$category_id) {
    $requestBody = $app->request()->getBody();
    $params = json_decode($requestBody, true);

    $agreement_id = $params['agreement_id'];

    $con = $this->conectarDB();
    $query = "CALL SPD_CATEGORIES($category_id,$agreement_id);";

    //print_r($query);

    $result = $this->con->query($query);    
    $this->con->close();
    $app->status(200);
    echo json_encode($result);
}

这是我在 /modules/folder/CategoriesView.js 上的主干删除功能

confirmDeleteForm : function(event) {
    event.preventDefault();
    var category_id = $('#categories_select').select2("val");
    var self=this;

    this.category = this.categories.findWhere({'category_id':category_id});
    var agreement_id = this.category.get('agreement_id');

    $('#deleteModal').modal('hide');

    this.category.destroy({
        data: {
            'agreement_id': agreement_id,
        },
        processData : true,         
        success: function(model, response) {

            self.cleanForm();
            self.getCategories();
        },
        error: function(model, response) {
            if (response.status == 500) {
                self.showErrorMessage('Se produjo un error en el servidor', 'ERROR');
            } else {
                self.showErrorMessage(response.responseText, 'ATENCION');
            }
        }
    });

},

在模型销毁中,我尝试添加 DATA: 和 HEADERS: 但没有任何效果。

有可能吗?

提前致谢。

更新 1:

我发现了这个:Slim v2 如第一个答案中所述。

还有这个:Backbone.js(BACKBONE.ROUTER 标题)

这就是我现在正在尝试的。我真的可以开始工作,但我认为这是解决我遇到的问题的方法。

更新 2

仍然可以弄清楚如何做到这一点。因此,由于我的截止日期快到了,我将数据库结构更改为仅使用行 ID 删除。 完成项目后我会继续尝试。

谢谢。

【问题讨论】:

  • 您也可以尝试添加 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8' 到标题。
  • +Davide Pastore 我还在尝试。我认为这是一种方式。谢谢。我会告诉你的

标签: javascript php jquery backbone.js slim


【解决方案1】:

你读过这个:http://docs.slimframework.com/routing/delete/#method-override吗?

很遗憾,现代浏览器不提供对 HTTP 的原生支持 删除请求。

...如果您使用 Backbone.js 或命令行 HTTP 客户端,您还可以使用 X-HTTP-Method-Override 标头覆盖 HTTP 方法。

【讨论】:

  • 感谢您的回答。我以前读过,但无法使它工作。你能在我的代码中给我一个例子吗?或者也许将我链接到它?提前致谢。
  • 老实说,我以前没有使用过 Backbone。我个人只是坚持使用GETPOST 方法用于Web 应用程序,并使用categories/1/delete 之类的参数来处理其他访问模式。我知道,这不是最 RESTful 的……但似乎是最透明和最直接的。我还是不明白为什么 HTML5 放弃了对DELETEPUT 的支持。
  • 顺便说一句,如果您将正文放在 DELETE 上,如果您尝试使用 Akamai 和其他各种 CDN,就会遇到麻烦。 Akamai 肯定不会费心通过 DELETE 的主体,因为 RESTful 约定是没有的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 2013-12-07
  • 2013-12-21
  • 2013-06-12
  • 1970-01-01
  • 2015-10-24
相关资源
最近更新 更多