我正试图通过POST将对象发送到PHP,并将其转换为关联数组
postData: function(word, description, translate) {
var formData = {
w: word,
d: description,
t: trasnlate
};
$http({
method: 'POST',
url: 'db.php',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: formData
}).
success(function(data, status, headers, config) {
console.log(data);
}).
error(function(data, status, headers, config) {
});
}
db.php
print_r($_POST);
console.log(数据(:
Array(
[{"w":"word","d":"description","h":"translate"}] => )
我想要这样的东西:
Array(
[w] => word
[d] => description
[t] => translate
)
在您的PHP代码中执行:
$json = file_get_contents('php://input');
$array = json_decode($json, true);
如果POST有效负载中提供了正确的JSON对象,则$array将是一个关联数组。