将数据从AJAX请求序列化到PHP
My problem is very common. I must be doing some silly mistake somewhere but I am not able to figure it out.
我的问题非常普遍。我必须在某处做一些愚蠢的错误,但我无法弄明白。
I am send my form data in serialized form but it is not coming to PHP at all.
我以序列化的形式发送我的表单数据,但它根本不是PHP。
Angular JS code:
Angular JS代码:
saveForm: function() {
var str = $('#feedbackForm').serializeArray();
alert(JSON.stringify(str)); // here I am getting my data properly
return $http({
method :'POST',
url:'http://localhost/api?module=form&app_id=APP001&action=save&formid=2&user_id=3',
data: str
});
}
PHP
$log->info($_REQUEST); // I am getting all GET parameters correctly
tried this also
也试过这个
$log->info($_POST);
it is not printing my data. why?
它不打印我的数据。为什么?
1 个解决方案
#1
2
By default, the $http
will send the data as application/json
, which won't be recognized by the $_POST
in PHP.
默认情况下,$ http会将数据作为application / json发送,PHP中的$ _POST将无法识别这些数据。
You have to choose either sending the data as form data like this:
您必须选择将数据作为表单数据发送,如下所示:
return $http({
method: 'POST',
url: 'http://localhost/api?module=form&app_id=APP001&action=save&formid=2&user_id=3',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: str
});
Or don't use the $_POST
but read and parse input directly in PHP like this:
或者不要使用$ _POST,而是直接在PHP中读取和解析输入,如下所示:
$rawInput = file_get_contents('php://input');
$data = json_decode($rawInput);
Hope this helps.
希望这可以帮助。
更多相关文章
- 在Servlet和HTML页面之间处理函数调用和数据传输的最佳方法是什
- 访问D3的var格式数据
- HTML/JS作为本地SQLite数据库的接口
- 如何使用条件if()使用javascript检索带有指定值的选择标记的xml数
- 使用Object.observe 实现数据绑定
- 环回 - 在“保存后”挂钩中覆盖之前查看数据
- 基于缓冲区数据创建文件
- 怎么javascript读取本地文件中的数据,并显示在html上
- 使用异步库汇编数据库中的所有标记