Javascript 将数据从AJAX请求序列化到PHP
我的问题很常见。我一定是在什么地方犯了什么愚蠢的错误,但我没能弄明白 我将以序列化的形式发送表单数据,但它根本不会发送到PHP 角JS代码:Javascript 将数据从AJAX请求序列化到PHP,javascript,php,ajax,angularjs,post,Javascript,Php,Ajax,Angularjs,Post,我的问题很常见。我一定是在什么地方犯了什么愚蠢的错误,但我没能弄明白 我将以序列化的形式发送表单数据,但它根本不会发送到PHP 角JS代码: saveForm: function() { var str = $('#feedbackForm').serializeArray(); alert(JSON.stringify(str)); // here I am getting my data properly return $http({ method
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($_POST);
它没有打印我的数据。为什么?默认情况下,
$http
将以应用程序/json
的形式发送数据,PHP中的$\u POST
不会识别该数据
您必须选择将数据作为表单数据发送,如下所示:
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
});
$rawInput = file_get_contents('php://input');
$data = json_decode($rawInput);
或者不要使用$\u POST
,而是直接在PHP中读取和解析输入,如下所示:
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
});
$rawInput = file_get_contents('php://input');
$data = json_decode($rawInput);
希望这能有所帮助。我也试过同样的方法……我不知道为什么它不起作用:(我可以按原样放置此PHP部件吗?或者我需要替换任何东西吗?因为我按原样复制了..它显示为空白。如果选择后者,则必须使用
$data
作为PHP json对象,例如$data->name
,$data->email
。它仍然无法工作:(…我序列化表单并将其发送到php的方式是正确的?。你能给我一些示例程序吗..那将非常有用。