存起来方便以后拷贝
关键代码如下:
.config(function($httpProvider) {
//定义配置文件
//转换angular默认的参数提交数据类型
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
})
//转换发送的参数数据类型
function buildPostParams(params){
var paramsArray = [];
Object.keys(params).forEach(function(key){
//console.log(params[key] instanceof Object);
if(params[key] instanceof Array){
params[key].forEach(function(val){
paramsArray.push(key+'['+params[key].indexOf(val)+']'+'=' + encodeURIComponent(val));
});
}else if(params[key] instanceof Object){
Object.keys(params[key]).forEach(function(keys){
paramsArray.push(key+'['+keys+']'+'=' + encodeURIComponent(params[key][keys]));
})
}else{
paramsArray.push(key + '=' + encodeURIComponent(params[key]));
}
})
params = paramsArray.join("&");
return params;
}
$scope.getToken=function(){
$scope.user='tjdx';
$scope.psw='abc123';
$scope.type='password';
// $scope.type='authorization_code';
$scope.apiInfo={
'grant_type': $scope.type,
'loginname': $scope.user,
'password': $scope.psw
};
$scope.apiInfo2=buildPostParams($scope.apiInfo);
console.log($scope.apiInfo2);
$http({
method: 'POST',
url: apiUrl2+'/api/oauth2/access_token',
data:$scope.apiInfo2,
dataType:'json',
headers : {'client_id' : clientId}
}).then(function successCallback(data) {
// 请求成功执行代码
console.log(data.data.access_token)
}, function errorCallback() {
// 请求失败执行代码
console.log('ERROR!')
});