我有这个邮递员的请求,但是有些角度等同于它的工作原理
我希望得到post man中确切请求的响应,如下所示。 在邮递员它给我我需要的东西,但不能让它在角度
var settings = {
"async": true,
"crossDomain": true,
"url": "http://197.157.93.110/Apiv1/token",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "c76c4b6a-9a69-23dd-0171-8e6cbacd7944",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "password",
"UserName": "user1",
"Password": "password"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我的角度代码看起来像这样
services.factory('documentService', ['$http', function($http){
return {
getToken:function(grant_type,user,password){
return $http({
method:"POST",
url:"http://197.156.93.110/Apiv1/token",
headers: {
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded"
},
data: {
"grant_type": grant_type,
"UserName": user,
"Password": password
}
});
}
,
我收到错误错误请求代码400
我以后在我的代码中使用它来调用这样的承诺
documentService.getToken("password","user1","password").success(function(token){
$scope.access_token=token.access_token;
$scope.userName=token.userName;
$scope.token_type=token.token_type;
$scope.message={"message":"success"};
$interval(function () {
$scope.message = "";
}, 10000);
//call get documents if only access token is success
}).error(function(err){
$scope.message=err;
$interval(function () {
$scope.message = "";
}, 10000);
});
答案 0 :(得分:0)
在您提供的代码中,您已启动了ajax调用并为Postman块提供了回调函数 - 也就是说,当ajax调用成功并返回数据时应该执行的操作。您还没有为AngularJS示例提供此功能。
$ http是一个基于承诺的包。您可以从General Usage下的Angular文档here中看到$ http应该使用.then()处理程序调用,如下所示:
var settings = {
//POST settings
}
$http(settings)
.then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response);
});
答案 1 :(得分:0)
只是转换请求并删除为我工作的标题上的缓存控件
最终代码
services.factory('documentService', ['$http', function($http){
return {
getToken:function(grant_type,user,password){
return $http({
method:"POST",
url:"http://197.156.93.110/Apiv1/token",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: {
"grant_type": "password",
"UserName": "user1",
"Password": "password"
}
});
}
,