Ionic 3应用向PHP页面发送数组数据的实现求助
解决Ionic 3数组数据发送到PHP的问题
我看了你的代码,问题出在直接把JS对象数组传给FormData——FormData没办法正确处理这种复杂结构,导致PHP那边接收到的不是你期望的数组,而是一堆[object Object]的字符串,自然没法用count()和循环处理。下面给你几个可行的解决方案:
方案1:把数组转成JSON字符串发送(最简便)
修改后的Ionic 3代码
this.listArray = [ {'title': 'testTitle'}, {'body': 'testbody'}, {'footer': 'testfooter'} ]; var headers = new Headers(); headers.append("Accept", "application/json"); let options = new RequestOptions({ headers: headers }); let body = new FormData(); // 关键:把数组转成JSON字符串再append body.append("file", JSON.stringify(this.listArray)); this.http.post("https://android.test.com/test.php", body, options) .map(res => res.json()) .subscribe(data => { console.log(data); alert("success") }, error => { console.log(error); alert("error") });
对应的PHP代码
// 先接收JSON字符串,再转成PHP数组 $jsonData = $_POST['file']; $fileArray = json_decode($jsonData, true); // true参数转成关联数组 if ($fileArray) { $arrayLength = count($fileArray); $message = "<p>".$arrayLength."</p>"; echo $message; foreach ($fileArray as $item) { // 遍历每个对象,输出键值 foreach ($item as $key => $value) { echo $key . ": " . $value . "<br />"; } } } else { echo "解析JSON失败"; }
方案2:用FormData逐个添加数组元素
如果不想用JSON,也可以循环数组,把每个元素以file[]为键添加到FormData里,这样PHP能直接识别为数组:
Ionic 3代码修改
this.listArray = [ {'title': 'testTitle'}, {'body': 'testbody'}, {'footer': 'testfooter'} ]; var headers = new Headers(); headers.append("Accept", "application/json"); let options = new RequestOptions({ headers: headers }); let body = new FormData(); // 循环数组,逐个添加到FormData this.listArray.forEach(item => { body.append("file[]", JSON.stringify(item)); }); this.http.post("https://android.test.com/test.php", body, options) .map(res => res.json()) .subscribe(data => { console.log(data); alert("success") }, error => { console.log(error); alert("error") });
PHP代码
if (isset($_POST['file']) && is_array($_POST['file'])) { $arrayLength = count($_POST['file']); echo "<p>".$arrayLength."</p>"; foreach ($_POST['file'] as $itemJson) { $item = json_decode($itemJson, true); if ($item) { foreach ($item as $key => $value) { echo $key . ": " . $value . "<br />"; } } } } else { echo "未接收到有效数组"; }
方案3:直接发送JSON请求体(无需FormData)
如果你的接口不需要处理文件上传,直接发送JSON请求体更简洁:
Ionic 3代码
this.listArray = [ {'title': 'testTitle'}, {'body': 'testbody'}, {'footer': 'testfooter'} ]; var headers = new Headers(); headers.append("Content-Type", "application/json"); // 关键:设置Content-Type let options = new RequestOptions({ headers: headers }); // 直接把数组作为请求体发送 this.http.post("https://android.test.com/test.php", JSON.stringify(this.listArray), options) .map(res => res.json()) .subscribe(data => { console.log(data); alert("success") }, error => { console.log(error); alert("error") });
PHP代码
// 从请求体读取JSON数据 $jsonData = file_get_contents('php://input'); $fileArray = json_decode($jsonData, true); if ($fileArray) { $arrayLength = count($fileArray); echo "<p>".$arrayLength."</p>"; foreach ($fileArray as $item) { foreach ($item as $key => $value) { echo $key . ": " . $value . "<br />"; } } } else { echo "解析JSON失败"; }
为什么原代码不行?因为当你执行body.append("file", this.listArray)时,FormData会自动把数组转成字符串,结果是"[object Object],[object Object],[object Object]",PHP接收到的就是这个字符串,而不是数组,所以count($_POST['file'])会返回1(因为是一个字符串),循环输出的话就是整个字符串内容,不是你要的数组元素。
内容的提问来源于stack exchange,提问作者Zakaria Zbir
相关产品推荐
相关产品推荐

