読者です 読者をやめる 読者になる 読者になる

AngularJSでPOST

$_POST['hoge']や$this->request->data['hoge']で受け取れない!!調べた結果下記の様になら取れる

postdata = file_get_contents("php://input");

request=json_decode(postada);

 AngularJS APIでPOSTのやり方(JS側ね)

<!doctype html>
<html land="ja" ng-app="myApp">
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>

//app
var app = angular.module('myApp',[]);

//controller
app.controller('myCtrl',function($scope,$http){

    $http.post(url,postdatas,{

    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},

    })
    .success(function(response){

        //response
        $scope.name = response.name;
        $scope.email = response.email;

    });

});
</script>
</head>
<body>
<div ng-controller="myCtrl">
{{name}}<br>
{{email}}
</div>
</body>
</script>

PHP側受け取る

<?php

    $name = "default";
    $email = "default@test.com";

    $postdata = file_get_contents("php://input");

    $request = json_decode($postdata);
header("Content-type: application/json"); echo json_encode($res);