Angularjs http请求

时间:2017-05-12 03:08:44

标签: javascript angularjs api oauth

我尝试申请angularjs

 POST /api/oauth/token HTTP/1.1
 Host: Myweb.com
 Authorization: Basic Base64(client_id:client_secret)
 Content-Type: application/x-www-form-urlencoded

 grant_type=client_credentials

喜欢这个..

 Var req = {
   method: 'POST',
   url: 'https://Myweb.com/api/oauth/token',
   headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': 'basic YjBiZGNkZTQtNmE5OS00ZDE4LTg5ZWQtYasdYtek0MzkTWCXy1LTRmOTktNDk0MS1iNDY2LTc2NDysdYdfsdxMzFiYg=='              
   },
   data: { 
           'grant_type':'client_credentials'                
         }     
 }

 $http(req).then(function successCallback(response) {
              console.log('Success');
            }, function errorCallback(response) {
              console.log('Failed');
            });

结果总是失败,以及如何正确应用于grant_type部分? 请帮帮我

2 个答案:

答案 0 :(得分:0)

请参考这段代码

var _login = function (loginData) {

        var data = "grant_type=password&username=" + 
        loginData.userName + "&password=" + loginData.password;

        var deferred = $q.defer();

        $http.post(serviceBase + 'token', data, { headers: 
        { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {

            localStorageService.set('authorizationData', 
            { token: response.access_token, userName: loginData.userName });

            _authentication.isAuth = true;
            _authentication.userName = loginData.userName;

            deferred.resolve(response);

        }).error(function (err, status) {
            _logOut();
            deferred.reject(err);
        });

        return deferred.promise; 
    };

它实际上取自 https://www.codeproject.com/articles/784106/angularjs-token-authentication-using-asp-net-web-a

答案 1 :(得分:0)

试试这段代码:

   var url="https://Myweb.com/api/oauth/token";    
    var data={ 
               'grant_type':'client_credentials'                
             };  

    $http.post(url,data,{ 
      headers:{
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': 'basic YjBiZGNkZTQtNmE5OS00ZDE4LTg5ZWQtYasdYtek0MzkTWCXy1LTRmOTktNDk0MS1iNDY2LTc2NDysdYdfsdxMzFiYg=='              
       }
    })
   .then(function(success){

   },function(failure){

   });