Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取访问令牌的角度post失败_Java_Angularjs_Rest_Http_Oauth - Fatal编程技术网

Java 获取访问令牌的角度post失败

Java 获取访问令牌的角度post失败,java,angularjs,rest,http,oauth,Java,Angularjs,Rest,Http,Oauth,你们能帮我创建一个angularjs POST请求版本吗 这个想法是获得一个访问令牌,这样我就可以安全地使用一些RESTAPI。 我通过angular尝试了很多次,我分析了每个请求,但都失败了,我总是发现请求正文是空的 下面是我尝试过的一些角度代码的示例 var app = angular.module("app", []); app .controller( "MyController", fun

你们能帮我创建一个angularjs POST请求版本吗

这个想法是获得一个访问令牌,这样我就可以安全地使用一些RESTAPI。 我通过angular尝试了很多次,我分析了每个请求,但都失败了,我总是发现请求正文是空的

下面是我尝试过的一些角度代码的示例

    var app = angular.module("app", []);
    app
        .controller(
                "MyController",
                function($scope, $http) {
                    $scope.obtain_token = function() {
                        var payload = "grant_type=password" + "&username=roy" + "&password=spring" +
                          "&client_id=clientapp"+
                          "&client_secret=123456";
                        var r = $http.post('http://localhost:8080/oauth/token',payload);
                        r.success(function(response){
                            console.log(response.access_token);
                        });
                    };
                });

你还应该解释什么是错的,你改变了什么。不仅张贴功能。(并且格式更好)他忘记了标题,你可以看到他在谷歌硬盘上附加的图片。adsl Le建议的代码在相同的应用程序中运行良好。问题是,我试图使用的资源托管在localhost:8080,我试图从另一个托管在localhost:8090的应用程序请求它。从前台可以吗
sample angular-1 code..
----------------------------------
var payload = {
"grant_type":"password",
"username"="roy",
"password"="spring",
"client_id"="clientapp"
"client_secret"="123456"
}

$http.post("http://localhost:8080/oauth/token", payload).success(function (result) {
                console.log(result);
            }).error(function(error) {
                console.log("error", error);
            });

and sample api c#
---------------------
[HttpPost]
        [Route("")]
        public HttpResponseMessage SaveArchive([FromBody] JObject saveRequest)
        {
            var log = LogManager.GetCurrentClassLogger();
            try``
            {
                return "your success message";
            }
            catch (Exception ex)
            {

                return ex;
            }
        }
    function Hello($scope, $http) {

    var config = {
            headers : {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': 'Basic ' +btoa('clientapp:123456654321')
            }
        }
    var payload ="password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp";

    $http.post('http://localhost:8080/oauth/token',payload, config).
    success(function(data, status, headers, config) {
        $scope.data = data;
    }).
    error(function(data, status, headers, config) {
        $scope.data = data; 
    }); 
}