AngularJS不通过POST发送数据

时间:2015-07-01 15:08:59

标签: javascript php angularjs

我正在尝试通过AngularJS $ http.post将一些表单数据保存到JSON,但不知何故它无效。这个想法非常简单:我有一个包含三个字段的表单,一个AngularJS数据绑定和一个非常简单的PHP代码,用于将其保存到文件中。不知怎的,它不起作用,因为PHP文件显然没有得到任何输入,所以它总是存储null

以下是代码:

HTML表单:

<form class="form-horizontal">
    <div class="form-group">
        <label for="title" class="col-sm-2 control-label">Title:</label>
        <div class="col-md-4">
            <input type="text" class="form-control" id="title" ng-model="book.title" placeholder="Title">
        </div>
    </div>
    <div class="form-group">
        <label for="pages" class="col-sm-2 control-label">Pages:</label>
        <div class="col-md-1">
            <input type="number" class="form-control input-sm" id="pages" ng-model="book.pages" placeholder="0">
        </div>
    </div>
    <div class="form-group">
        <label for="readPages" class="col-sm-2 control-label">Read pages:</label>
        <div class="col-md-1">
            <input type="number" class="form-control input-sm" id="readPages" ng-model="book.readPages" placeholder="0">
        </div>
    </div>
    <button class="btn btn-primary glyphicon glyphicon-floppy-save" ng-click="save()"></button>
</form>

JS代码:

angular.module('book', [
    'ui.router'
])
    .config(function config($stateProvider) {
        $stateProvider.state('book', {
            url: '/book',
            views: {
                "main": {
                    controller: 'Book',
                    templateUrl: 'ui/views/book.tpl.html'
                },
                "header": {
                    templateUrl: 'ui/views/header.tpl.html'
                },
                "footer": {
                    templateUrl: 'ui/views/footer.tpl.html'
                }
            },
            data: {pageTitle: 'Book Status'}
        });
    })

    .controller('Book',
    function ($scope, $http) {
        $scope.book = {};
        $scope.error = "";

        $scope.save = function() {
            console.log($scope.book);
            $http.post('save/book.php', $scope.book).then(function(response) {
                // log success
                console.log("Guardado con éxito.");
                console.log(response);
              });
        };
});

PHP代码:

$data = file_get_contents("php://input");
$inp = file_get_contents('../data/books.json');

$tempArray = json_decode($inp);
$tempData = json_decode($data);
array_push($tempArray, $tempData);
$jsonData = json_encode($tempArray);
$file = fopen('../data/books.json','w+');
fwrite($file, $jsonData);
fclose($file);
echo $jsonData;

所以,显然一切都是正确的,但我得到了这个回应:

  

(!)警告:array_push()期望参数1为数组,为null   在第7行的C:\ wamp \ www \ angulardemos \ save \ book.php中给出

这告诉我输入不正确,所以我实际上并没有发送任何数据。我错过了什么?

2 个答案:

答案 0 :(得分:0)

easy $tempArray为null add var_dump to debug it

$tempArray = $tempArray = json_decode($inp, true);
$tempData = json_decode($data);
array_push($tempArray, $tempData);

您也可以尝试:

$tempArray[]=$tempData;

答案 1 :(得分:0)

从ng-model中删除书籍并将其转换为    <input type="text" class="form-control" id="title" ng-model="title" placeholder="Title">

同样适用于所有...和保存功能 var dataObj = { title: $scope.title, pages: $scope.pages
}
var res = $http.post('save/book.php', JSON.stringify(dataObj)