简体   繁体   English

带有自定义数据的Angular HTTP请求

[英]Angular http request with custom data

I am trying to do a http request to a php file on my server. 我正在尝试对服务器上的php文件执行http请求。 My code i am using at the moment is as follows: 我目前正在使用的代码如下:

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;
        });
}]);

Isnt there a better way to do this? 有没有更好的方法可以做到这一点? When i add these var as data it doesnt get posted to my page? 当我将这些var添加为数据时,它不会发布到我的页面上吗?

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

In php i get the data like this, its also sanitized for security reason: 在PHP中,我得到这样的数据,出于安全原因也对其进行了清理:

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

At first sight you are tryng to call an HTTP POST service, but you send parameter like it was a GET service, try something like that: 乍一看,您正在尝试调用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);
        });

I would use json_decode( file_get_contents('php://input') ) on your serverside. 我会在服务器端使用json_decode( file_get_contents('php://input') ) Also, please don't forget to sanitize your user sent data! 另外,请不要忘记清理用户发送的数据!

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;
        });
}]);

You will want to watch ever using the variable data as it will most likely collide with another variable, such as in your demonstration where you have named your post params as data while the return response is also aliased as data in the $.post success. 您将要观看使用变量data因为它很可能与另一个变量发生冲突,例如在您的演示中,您已将post params命名为data而返回响应在$ .post成功中也被别名为data This may not cause an issue in this case - but it usually will, so I renamed it for you out of habit. 在这种情况下,这可能不会引起问题-但通常会造成问题,因此出于习惯,我为您重命名了它。

Your server side could look something like this depending on what your usernames strings consist of: 您的服务器端可能看起来像这样,具体取决于您的用户名字符串组成:

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'))); 

Now you can access the username like: 现在您可以访问用户名,例如:

echo $client_data['user']; // Will echo 1 based on the post data you are sending //将根据您发送的帖子数据回显1

This is what a simple serverside data-router could look like, as using normal $_POST has never worked for Angular data for me either: 这就是一个简单的服务器端数据路由器的样子,因为使用普通的$ _POST从来都不对Angular数据有效:

    /**
    * 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);

I was informed of this by: http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/ and How to post a data in Angular? 通过以下方式通知我: http : //www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/以及如何在Angular中发布数据? .

The point is... use $client_data = json_decode( file_get_contents('php://input') ); 关键是...使用$client_data = json_decode( file_get_contents('php://input') ); instead of $client_data = $_POST['username']; 而不是$client_data = $_POST['username'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM