繁体   English   中英

使用Slim Framework无法从DELETE请求中获取正文

[英]Cannot get body from DELETE request with Slim Framework

我正在使用Beta.js和Slim Framework开发应用程序。 问题在这里:我需要通过执行一个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的数据库。 完成项目后,我将继续尝试此操作。

谢谢。

您已阅读以下内容: http : //docs.slimframework.com/routing/delete/#method-override吗?

不幸的是,现代浏览器不提供对HTTP DELETE请求的本机支持。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM