我正在使用 $http 通过 angularjs 将数据发送到 PHP 文档 , 该文档旨在保存 MySQL 数据库中的数据。 然而 , 数据被解码为空白或未定义。 JSON 将它添加到 PHP 文件中 , 因为我可以看到请求标题 , 但是响应是空的。
我尝试过测试代码的不同变体 , 以确保 JSON 编码的数据能够实现 PHP 文档 , 而且确实如此 , 但是当尝试 json_decode()
时 , 它不会从 JSON 中提取任何东西。
PHP 文件
- $postdata = file_get_contents("php://input");
- $request = json_decode($postdata);
- $user = $request->Username;
- echo $user;
安哥拉js
- $scope.submit = function() {
- $http({
- url: "http://www.walkermediadesign.com/planner3/src/ceremony.php",
- method: "POST",
- data: this.ceremony
- }).then(function successCallback(response) {
- console.log(response.data);
- }, function errorCallback(response) {
- $scope.error = response.statusText;
- })};
这是post数据 :
- $postdata =
- (2) [{…}, {…}]
- 0: {Username: "redphyre@gmail.com"}
- 1: {opening: "Friends and Family of BRIDE and GROOM, welcome and…d
- falling in love with each other all over again."}
- length: 2
- __proto__: Array(0)
没有错误消息或 500 个错误, 只返回空白数据。
-------------------------------------
我想你是在期待像这样的约翰逊数据 :
- {
- "Username": "redphyre@gmail.com",
- "opening": "Friends and Family..."
- }
在那里你有一个具有所有预期属性的对象。
然而 , 你真正得到的是 :
- [
- { "Username": "redphyre@gmail.com" },
- { "opening": "Friends and Family..." }
- ]
这会创建一个对象数组 , 每个对象都只有一个属性 , 这几乎不太容易处理。 要将数据转换为具有多个属性的单个对象 , 可以通过结果集循环 :
- $responseData = new stdClass();
- foreach ($response as $propertyObject) {
- $properties = get_object_vars($propertyObject);
- // Just in case some objects have more than one property after all
- foreach($properties as $name => $value) {
- $responseData->$name = $value;
- }
- }
将响应数组中的对象的单个属性复制到单个对象中。
记录下,From: http://qurl.cc/068/ShuJuShiTongGuo-http-FaSongDao-php-WenJian-DanShi-JSON-JieMaKongBaiHuoWeiDingYi/