589

httpPOSTリクエストを行う関数があります。コードは以下のとおりです。これは正常に機能します。

 $http({
   url: user.update_path, 
   method: "POST",
   data: {user_id: user.id, draft: true}
 });

http GET用の別の関数があり、そのリクエストにデータを送信したいと思います。しかし、getにはそのオプションがありません。

 $http({
   url: user.details_path, 
   method: "GET",
   data: {user_id: user.id}
 });

の構文http.get

get(url、config)

4

7 に答える 7

956

HTTP GETリクエストには、サーバーに投稿するデータを含めることはできません。ただし、リクエストにクエリ文字列を追加することはできます。

angle.httpは、と呼ばれるオプションを提供しますparams

$http({
    url: user.details_path, 
    method: "GET",
    params: {user_id: user.id}
 });

参照:http ://docs.angularjs.org/api/ng.$http#getおよびhttps://docs.angularjs.org/api/ng/service/$http#usage(パラメーターを表示params

于 2012-12-07T09:34:11.507 に答える
526

パラメータを直接に渡すことができ$http.get()ます以下は正常に動作します

$http.get(user.details_path, {
    params: { user_id: user.id }
});
于 2013-08-29T18:07:23.243 に答える
44

AngularJS v1.4.8以降get(url, config) 次のように使用でき ます。

var data = {
 user_id:user.id
};

var config = {
 params: data,
 headers : {'Accept' : 'application/json'}
};

$http.get(user.details_path, config).then(function(response) {
   // process response here..
 }, function(response) {
});
于 2015-12-28T07:04:06.087 に答える
34

GETリクエストでパラメータとヘッダーを送信することに興味がある人のためのソリューション

$http.get('https://www.your-website.com/api/users.json', {
        params:  {page: 1, limit: 100, sort: 'name', direction: 'desc'},
        headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
    }
)
.then(function(response) {
    // Request completed successfully
}, function(x) {
    // Request error
});

完全なサービス例は次のようになります

var mainApp = angular.module("mainApp", []);

mainApp.service('UserService', function($http, $q){

   this.getUsers = function(page = 1, limit = 100, sort = 'id', direction = 'desc') {

        var dfrd = $q.defer();
        $http.get('https://www.your-website.com/api/users.json', 
            {
                params:{page: page, limit: limit, sort: sort, direction: direction},
                headers: {Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
            }
        )
        .then(function(response) {
            if ( response.data.success == true ) { 

            } else {

            }
        }, function(x) {

            dfrd.reject(true);
        });
        return dfrd.promise;
   }

});
于 2015-12-11T15:39:39.903 に答える
4

URLの最後にパラメータを追加することもできます。

$http.get('path/to/script.php?param=hello').success(function(data) {
    alert(data);
});

script.phpとペアリング:

<? var_dump($_GET); ?>

次のJavaScriptアラートが発生します。

array(1) {  
    ["param"]=>  
    string(4) "hello"
}
于 2014-09-25T11:31:59.430 に答える
2

ASP.NETMVCでangular.jsを使用するパラメーターを使用したHTTPGETリクエストの完全な例を次に示します。

コントローラ:

public class AngularController : Controller
{
    public JsonResult GetFullName(string name, string surname)
    {
        System.Diagnostics.Debugger.Break();
        return Json(new { fullName = String.Format("{0} {1}",name,surname) }, JsonRequestBehavior.AllowGet);
    }
}

見る:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script type="text/javascript">
    var myApp = angular.module("app", []);

    myApp.controller('controller', function ($scope, $http) {

        $scope.GetFullName = function (employee) {

            //The url is as follows - ControllerName/ActionName?name=nameValue&surname=surnameValue

            $http.get("/Angular/GetFullName?name=" + $scope.name + "&surname=" + $scope.surname).
            success(function (data, status, headers, config) {
                alert('Your full name is - ' + data.fullName);
            }).
            error(function (data, status, headers, config) {
                alert("An error occurred during the AJAX request");
            });

        }
    });

</script>

<div ng-app="app" ng-controller="controller">

    <input type="text" ng-model="name" />
    <input type="text" ng-model="surname" />
    <input type="button" ng-click="GetFullName()" value="Get Full Name" />
</div>
于 2016-05-18T07:32:16.157 に答える
1

パラメータを使用してgetリクエストを送信するには、

  $http.get('urlPartOne\\'+parameter+'\\urlPartTwo')

これにより、独自のURL文字列を使用できます

于 2016-03-30T07:03:03.867 に答える