0

I'm doing a web app. Right nowI'm doing the authentication page. The server is done with Django. But the server expects a json. How can I convert and what I have to convert before send data? Thanks!

This is the Authentication service code:

'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();
  }
};

});

1 Answer 1

1

This should work:

var obj = {
    email:email,
    pwd:pwd
}
$http.post(url, obj).then() 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.