我正在使用角度http客户端与数据库进行交互,并且一切正常,但是当我尝试使用表单将数据发布到同一链接时,我得到的数据是未定义的。
我正在尝试对值进行编码和解码,因为我知道在发出任何POST请求和发送数据之前,我需要执行angular.toJSON方法,但这没有用。
这是我的index.php,我从该表单接收POST请求。
if (empty($action)) {
if ((($_SERVER['REQUEST_METHOD'] == 'POST')) &&
(strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false)) {
$input = json_decode(file_get_contents('php://input'), true);
$action = isset($input['action']) ? $input['action'] : null;
$subject = isset($input['subject']) ? $input['subject'] : null;
$data = isset($input['data']) ? $input['data'] : null;
}
case 'createNote':
// if I die() here, it prints the die()
if(!empty($data)) {
// if I die() here, $data is undefined.
$data = json_decode($data);
$user = $data[0];
$comment = $data[1];
$film_id = $data[2];
$lastupdated = date("Y-m-d H:i:s");
$sql = "INSERT INTO nfc_note (user, film_id, comment, lastupdated)
VALUES (:user, :film_id, :comment, :lastupdated)";
}
break;
我用来发送POST请求的表格
">
如上所述,当我使用angular的http并传递如下参数时,它可以工作:
this.createNote = function (data) {
var defer = $q.defer(),
data = {
action: "create",
subject: "note",
data: angular.toJson(data)
};
$http
.post(urlBase, data)
.success(function (response) {
defer.resolve({
data: response.data
});
})
.error(function (error) {
defer.reject(error);
});
return defer.promise;
};
使用表格时不起作用。我没有发现任何建议或错误?