简体   繁体   English

带标题的$ http角登录帖子

[英]angular $http post login with headers

i am new to AngularJS and i am encountering an issue with the system i am developing. 我是AngularJS的新手,我正在开发的系统遇到问题。

i got this error 我得到这个错误

ReferenceError: $http is not defined ReferenceError:未定义$ http

when i tried to use this code. 当我尝试使用此代码时。

 'use strict'; myApp.factory('loginService', function() { return { login: function(user) { var $promise = $http.post( 'http://unexus-api-dev-3urcgetdum.elasticbeanstalk.com/users/login', { headers: { 'Authorization': 'Token token=\\"bc0a3f1cfeaf2d9ad98edd005a756ae9\\"' }, user }); $promise.then(function(msg) { if (msg.data == 'success') console.log('success login'); else console.log('failed login'); }); } } }); 

i know that my code is messy but i hope you guys can help me. 我知道我的代码很乱,但我希望你们能帮助我。

You have pass the dependency $http in your factory. 您已经在工厂中通过了依赖项$ http。

myApp.factory('loginService',['$http', function($http) {
  return {
    login: function(user) {
      var $promise = $http.post(
        'http://unexus-api-dev-3urcgetdum.elasticbeanstalk.com/users/login', {
          headers: {
            'Authorization': 'Token token=\"bc0a3f1cfeaf2d9ad98edd005a756ae9\"'
          },
          user
        });

      $promise.then(function(msg) {
        if (msg.data == 'success')
          console.log('success login');
        else
          console.log('failed login');
      });
    }
  }
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM