【发布时间】:2018-04-26 09:03:14
【问题描述】:
var send=[];
// send[] data contains:
// [{"host":"1","name":"2","password":"3"},
// {"host":"4","name":"5","password":"6"},
// {"host":"7","name":"8","password":"9"},
// {"host":"10","name":"11","password":"12"}]
$http({
method: 'POST',
url: 'file.php',
data: send,
headers: {
'Content-Type': 'Content-type: application/json'
},
}).
success(function(response) {}).
error(function(response) {});
return false;
};
我在发送数据时遇到错误,我无法使用 $_POST 解析这些数据。我尝试过以下操作:
var obj=(JSON.stringify(send));
data:obj,
在我的 PHP 文件中:
$request = file_get_contents('php://input');
$data=json_decode($request);
print_r($data);
【问题讨论】:
-
您遇到了什么错误? (有
$http.post("file.php", send).then(function(response){ ... })就够了)你也可以尝试用echo json_encode($data)返回数据 -
来自本地主机:
通知:未定义索引:/Applications/XAMPP/xamppfiles/htdocs/try/parse.php 中的名称上线2
注意:未定义索引:/Applications/XAMPP/xamppfiles/htdocs/try/parse.php中的密码 第 3 行
– -
这就是我得到的响应,当我这样做时
success(function(response) { window.alert(response); -
您使用的是 Mac 吗?您不应该使用MAMP 本地服务器而不是XAMPP 吗?但这绝对是后端问题
-
如果您将
'Content-Type': 'Content-type: application/json'更改为'Content-Type': 'application/json',您的代码是否有效
标签: javascript php arrays angularjs json