我正在做一个网络应用程序。现在我正在进行身份验证页面。服务器是用Django完成的。但服务器期望一个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();
}
};
});
答案 0 :(得分:1)
这应该有效:
var obj = {
email:email,
pwd:pwd
}
$http.post(url, obj).then()