2015-07-12 50 views
2

我正在尝试对我的服务器上的php文件执行http请求。我现在使用的代码如下:有自定义数据的角度http请求

App.controller('GetSales', ['$scope', '$http', function ($scope, $http) { 

     $http({ 
      method: 'POST', 
      url: '/app/controller/apis/_sales.php?period_start=2015-07-01&period_end=2015-07-31&join=leads&status=0&category=0user=1' 
     }) 
     .success(function (data) { 
      $scope.sales = data; 
     }); 
}]); 

还没有更好的方法来做到这一点? 当我将这些var作为数据添加它不会发布到我的页面?

data: { 
      period_start: '2015-07-01', 
      period_end: '2015-07-31', 
      join: 'leads', 
      status: '', 
      category: '', 
      user: '1' 
     }; 

在PHP时得到这样的数据,其也消毒出于安全原因:

$user = filter_var($_REQUEST['user'], FILTER_SANITIZE_NUMBER_INT); 
$period_start = $_REQUEST['period_start'].' 00:00:00'; 
+0

我的回答已被更新,这是用来访问AngularJS不正确的API。查看我的演示并链接到$ http的Angular Docs。这是使用jQuery风格,但与Angular,不会工作。 –

+0

你的问题不是客户端在这一点上,我延伸我的答案低于 –

+0

那么我坐在这已经像“永远”,我不知道什么是失踪? –

回答

0

在你tryng调用一个HTTP POST服务一见钟情,但你送参数就像是一个GET服务,尝试类似的东西:

App.controller('GetSales', ['$scope', '$http', function ($scope, $http) { 
    $http.post('/app/controller/apis/_sales.php', 
     { 
      period_start: '2015-07-01', 
      period_end: '2015-07-31', 
      join: 'leads', 
      status: '', 
      category: '', 
      user: '1' 
     }) 
     .success(function (data) { 
      $scope.sales = data; 
     }) 
     .error(function (data, status) { 
      console.log(status); 
     }); 
+0

我已经试过了,但它不起作用!你的代码也有一些错误! –

+0

是的,我忘了分号,但我认为是PHP服务器端的问题。将一个错误子句放到$ http服务调用中,并捕获错误 –

+0

,它不会拖延任何错误,而是返回“null”...? –

0

我会在你的服务器端使用json_decode(file_get_contents('php://input'))。另外,请不要忘记清理您的用户发送的数据!

var dataParams = { 
      period_start: '2015-07-01', 
      period_end: '2015-07-31', 
      join: 'leads', 
      status: '', 
      category: '', 
      user: '1' 
}; 
App.controller('GetSales', ['$scope', '$http', function ($scope, $http) { 
     $http.post('/app/controller/apis/_sales.php', dataParams) 
     .success(function (data) { 
      $scope.sales = data; 
     }); 
}]); 

您将要使用的变量data因为这将最有可能与另一个变量冲突,如在您的演示观看过,你命名你的post数据为data而返回的响应也化名为data在$ .post成功。这可能不会引起这种情况下的问题 - 但通常情况下,所以我将它改名为您习惯。

你的服务器端可能看起来像这取决于你的用户名字符串包含的是什么东西:

public static function sanatize_client_string($dirtyString){ 
        $cleanString = htmlspecialchars(strtolower(preg_replace("/[^a-z]+/i", "[FORBIDDEN_CHAR]", $dirtyString))); 
        return $cleanString; 
       } 

$client_data = sanatize_client_string(json_decode(file_get_contents('php://input'))); 

现在,您可以访问诸如用户名:

echo $client_data['user']; //将根据岗位回声1你正在发送的数据

这是一个简单的服务器端数据路由器的样子,因为使用普通的$ _POST从来没有为我的角数据工作:

How to post a data in Angular?:210
/** 
    * Collect all Angular HTTP Request data 
    */ 
    $client_data = json_decode(file_get_contents('php://input')); 

    $app_state = utils::sanatizeClientString($client_data->appState); // <- name of prop must match client angularPostObj.x = serverModel.x 

    /** 
    * Cache the view path to the model 
    */ 
    $module_exists_in_model = isset($app['view_data']['views'][$app_state]); 

    /** 
    * If the angular post request matches data in the model, return the requested dataset, while if no object is found 
    * under that address, the error object will be returned which will send and error data to the view. 
    * 
    * This error condition will never happen aside from an attack because the clientside AngularJS router would prevent any 
    * unregistered paths from being even sent to the server. This would happen using a post mocking service or by 
    * forcing the string change in the code inspector while using the site. 
    */ 
    $module_exists_in_model ? 
     $view_model = $app['view_data']['views'][$app_state] : 
     $view_model = $app['view_data']['views']['error']; 

    // Call the view from Angular post data, passing it to a Class that sends a response as valid JSON 

    Render_app::echo_json($view_model); 

我被告知此。

的一点是...使用$client_data = $_POST['username'];$client_data = json_decode(file_get_contents('php://input'));代替

+0

我已经尝试过,但它不起作用!你的代码也有一些错误! –

+0

对不起,你的jquery喜欢通过我的语法,你的权利,这是不正确的方式来调用角$ http.post。当使用$ http作为角度https://docs.angularjs.org/api/ng/service/$http –

+0

基本上你的演示有两个问题,一个是你试图发送使用$ post作为查询字符串就像@ marianoc84指出的那样,'get style',你的代码使用jQuery风格的api来访问$ http服务 - 这也是我所追求的。容易混淆,希望代码适用于亚! –