【发布时间】: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