0
我在做一個web應用程序。現在我正在做認證頁面。服務器用Django完成。但服務器期望一個JSON。如何在發送數據之前進行轉換和轉換?謝謝!在發送數據以進行身份驗證之前轉換爲Json
這是認證服務代碼:
'use strict';
app.factory('AuthService', function ($http, $window, $q, API_SERVER) {
var authenticate = function (email, pwd, endpoint) {
var url = API_SERVER + endpoint;
var deferred = $q.defer();
$http.post(url, 'email=' + email + '&pwd=' + pwd, {
headers: {
'Content-Type': 'application/json'
}
}).then(
function (response) {
var token = response.data.token;
var email = response.data.email;
if (token && email) {
$window.localStorage.token = token;
$window.localStorage.email = email;
deferred.resolve(true);
} else {
deferred.reject('Invalid data received from server');
}
},
function (response) {
deferred.reject(response.data.error);
}
);
return deferred.promise;
};
var logout = function() {
var deferred = $q.defer();
var url = API_SERVER + 'logout/';
$http.post(url).then(
function() {
$window.localStorage.removeItem('token');
$window.localStorage.removeItem('email');
deferred.resolve();
},
function (error) {
deferred.reject(error.data.error);
}
);
return deferred.promise;
};
return {
register: function (email, pwd) {
return authenticate(email, pwd, 'accountmanager/');
},
login: function (email, pwd) {
return authenticate(email, pwd, 'login/');
},
logout: function() {
return logout();
}
};
});
不幸的是它不工作:/ – panagulis72
我一定要編輯 '登錄' 功能嗎? – panagulis72