Angularjs 我的angular应用程序中的错误请求代码400
我从正在进行的api调用中收到了一个错误的请求,我不知道为什么,已经搜索了很长时间了。我从电话中得到的答复是:Angularjs 我的angular应用程序中的错误请求代码400,angularjs,web-services,rest,http,http-headers,Angularjs,Web Services,Rest,Http,Http Headers,我从正在进行的api调用中收到了一个错误的请求,我不知道为什么,已经搜索了很长时间了。我从电话中得到的答复是: {"error":"invalid_request","error_description":"Missing input parameters"} 因此,我将在下面列出我的代码,首先是我的html表单: <ion-view view-title="Login" id="login-page"> <ion-content id="login-page"&
{"error":"invalid_request","error_description":"Missing input parameters"}
因此,我将在下面列出我的代码,首先是我的html表单:
<ion-view view-title="Login" id="login-page">
<ion-content id="login-page">
<h1 id="title">Welkom</h1>
<img src="../img/logo.png" alt="logo" id="logo">
<form>
<div class="list list-inset">
<label class="item item-input">
<input type="email" ng-model="email" name="email" placeholder="email">
</label>
<label class="item item-input">
<input type="password" ng-model="password" name="password" placeholder="wachtwoord">
</label>
</div>
<button class="button button-block button-balanced" type="submit" id="form-btn" ng-click="login()">Log in</button>
<button class="button button-block button-stable" type="submit" id="form-btn">Registreer</button>
<button class="button button-block button-dark" type="submit" id="scan-btn">Scan</button>
<a href="templates/browse.html">test</a>
</form>
</ion-content>
</ion-view>
最后,我的控制器:
angular.module('starter.controllers', [])
.controller('LoginCtrl', function($scope, $location, $localStorage, loginFactory) {
$scope.login = function() {
var email = $scope.email;
var password = $scope.password;
//test BAD REQUEST 400 console.log(email, password)
loginFactory.login(email, password)
.then(function(response){
console.log(response);
$localStorage.token = response.access_token;
$stage.go("app.browse")
}, function(error) {
$scope.email = '';
$scope.password = '';
} )
}
});
我的app.js只是路由。所以我尝试获取一个令牌,因为我必须使用这个令牌来执行其他api调用
这是api信息:
描述
参数
响应
根据用户的凭据(电子邮件和密码)请求访问令牌。为了获得授权,必须在所有其他API调用中使用返回的访问令牌。需要使用方法“POST”调用Web服务
'grant_type=“password”
用户名=用户的电子邮件地址
password=用户的密码
client_id=应用程序的id
client_secret=应用程序的密钥
访问令牌是一个64个字符的字符串。
访问令牌也有一个过期时间。只有在过期时间过后,才能使用令牌。
对于将来遇到此问题的任何人:经过长时间的调试,我找到了解决方案:
这实际上是唯一有效的解决方案,我尝试了许多$http语法。但只有这个特定的方法有效
.factory('loginFactory', function($http, $q, $localStorage){
return {
login: function(email, password) {
var deferred = $q.defer();
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}};
var url = "https://www.gingerwald.com/community/v2.1/authorization/oauth/token.php";
$http.post(url, "username=" + encodeURIComponent(email) +
"&password=" + encodeURIComponent(password) +
"&grant_type=password" +
"&client_id=GingerwaldUserApp15" +
"&client_secret='secretKey'", config)
.success(function(respons){
deferred.resolve(respons);
})
.error(function(respons,status) {
deferred.reject(respons);
console.log()
})
return deferred.promise;
}
}
})
如果在以前的尝试中指定导致问题的原因,则会更有帮助。
.factory('loginFactory', function($http, $q, $localStorage){
return {
login: function(email, password) {
var deferred = $q.defer();
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}};
var url = "https://www.gingerwald.com/community/v2.1/authorization/oauth/token.php";
$http.post(url, "username=" + encodeURIComponent(email) +
"&password=" + encodeURIComponent(password) +
"&grant_type=password" +
"&client_id=GingerwaldUserApp15" +
"&client_secret='secretKey'", config)
.success(function(respons){
deferred.resolve(respons);
})
.error(function(respons,status) {
deferred.reject(respons);
console.log()
})
return deferred.promise;
}
}
})