2014-05-07 101 views
1

在我的角度應用程序中,我使用slim框架作爲服務器。粘到從Slim_Http_Request對象身體值,如何從Slim_Http_Request對象中獲取「body」對象的值

我app.js代碼段,

MyApp.controller('Remove', function($scope,$http,$location) { 
$scope.del = function(id) { 
    var data = {id:id}; 
    console.log(id); 
    $http.post('api/index.php/del_user',data).success(function(){ 
    }); 
    $scope.reset(); 
} 
}); 

我的觀點,

<tbody> 
<tr ng-repeat="user in users"> 
    <td>{{user.username}}</td> 
    <td>{{user.first_name}}</td> 
    <td>{{user.last_name}}</td> 
    <td>{{user.address}}</td> 
    <td><a class="btn btn-primary" ng-controller="Remove" ng-click="del(user.id)">Del</a>   </td> 
</tr> 

當我打印,

$request = Slim::getInstance()->request(); 
echo '<pre>'; 
print_r($request);die; 

有迴應E在XHR,

<pre>Slim_Http_Request Object 
(
[method:protected] => POST 
[headers:protected] => Array 
    (
     [host] => localhost 
     [connection] => keep-alive 
     [content-length] => 10 
     [accept] => application/json, text/plain, */* 
     [origin] => http://localhost 
     [x-requested-with] => XMLHttpRequest 
     [user-agent] => Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 
     [content-type] => application/json;charset=UTF-8 
     [referer] => http://localhost/ag_test/ 
     [accept-encoding] => gzip,deflate,sdch 
     [accept-language] => en-US,en;q=0.8,he;q=0.6,es;q=0.4,gu;q=0.2 
     [cookie] => request_method=GET; _funkyfish_session=TTZtakRpQnd3WGl5SW9INklFZmd0dUdrV2JlTWdYZmo3YmpPazdKVlBtSzRQSHZYdlR5ZzFza2UzWUY0V2Q0clRQakJZMU1OS0FlTUdlemNzMzlIcFZFWDlmN2dVVGxNcUpLZzNRNHp4TVNpRm4vQVNxcWhwNUdhS0JDczZkOTVsZWZ0NUZZTzZZbllzK1Ntem1pVG1hSi9zeVErVWxUSk9rTlE3WVhzV005SmgzZ2NIQ3RMV0FrZEdtSFBoWHFLaFk4d2FlNnVVNkd6TVRkQU5pMGVEWGYvSi8wWk9TT0ltM2lWSlBpaTc5KzhqNy9oVDczQThZNHVvbUVEZVZVVDltZ0Vqa3R5OGpTdTVIR1VveVRsU1E9PS0tb3N0RGUvZFpNNXNlKzRXaE5tUXJvUT09--e090007458a370fcfe4a51ca4b55aa2dca6417cb; PHPSESSID=5c2a395dee4c4ed55d2885d2d6184527; 5c2a395dee4c4ed55d2885d2d6184527=DEFAULT%7C0%7C2M3TMlgUx3gTlaarYzHIdD28l8q9FTcNubt55%2BUGpAo%3D%7C7456bf61db3500c8bb7b3bc38082a470ce4a2ad3 
    ) 

[additionalHeaders:protected] => Array 
    (
     [0] => content-type 
     [1] => content-length 
     [2] => php-auth-user 
     [3] => php-auth-pw 
     [4] => auth-type 
     [5] => x-requested-with 
    ) 

[cookies:protected] => Array 
    (
     [request_method] => GET 
     [_funkyfish_session] => TTZtakRpQnd3WGl5SW9INklFZmd0dUdrV2JlTWdYZmo3YmpPazdKVlBtSzRQSHZYdlR5ZzFza2UzWUY0V2Q0clRQakJZMU1OS0FlTUdlemNzMzlIcFZFWDlmN2dVVGxNcUpLZzNRNHp4TVNpRm4vQVNxcWhwNUdhS0JDczZkOTVsZWZ0NUZZTzZZbllzK1Ntem1pVG1hSi9zeVErVWxUSk9rTlE3WVhzV005SmgzZ2NIQ3RMV0FrZEdtSFBoWHFLaFk4d2FlNnVVNkd6TVRkQU5pMGVEWGYvSi8wWk9TT0ltM2lWSlBpaTc5KzhqNy9oVDczQThZNHVvbUVEZVZVVDltZ0Vqa3R5OGpTdTVIR1VveVRsU1E9PS0tb3N0RGUvZFpNNXNlKzRXaE5tUXJvUT09--e090007458a370fcfe4a51ca4b55aa2dca6417cb 
     [PHPSESSID] => 5c2a395dee4c4ed55d2885d2d6184527 
     [5c2a395dee4c4ed55d2885d2d6184527] => DEFAULT|0|2M3TMlgUx3gTlaarYzHIdD28l8q9FTcNubt55+UGpAo=|7456bf61db3500c8bb7b3bc38082a470ce4a2ad3 
    ) 

[get:protected] => Array 
    (
    ) 

[post:protected] => Array 
    (
    ) 

[put:protected] => Array 
    (
    ) 

[body:protected] => {"id":"8"} 
[contentType:protected] => application/json 
[resource:protected] => /del_user 
[root:protected] => /ag_test/api/index.php 
) 

回答

0

如果你想獲得的請求主體的JSON對象,你應該能夠通過使用

$body = Slim::getInstance()->request()->getBody(); 
$id = $body["id"]; 

得到這個在修身不知道如果這是你想要做的?