Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restangular request has problem #1

Open
woshihaoren opened this issue Mar 27, 2015 · 0 comments
Open

restangular request has problem #1

woshihaoren opened this issue Mar 27, 2015 · 0 comments

Comments

@woshihaoren
Copy link

Blog.service('AuthService', function($q, $http, localStorageService, Session, Restangular) {

    this.login = function(credentials) {
        var me = this;
        deferred = $q.defer()
        Session.create(credentials, true).then(function(user) {
            me.setToken(credentials);
            return deferred.resolve(user);
        }, function(response) {
            if (response.status == 401) {
                return deferred.reject(false);
            }
            throw new Error('No handler for status code ' + response.status);
        });
        return deferred.promise
    };

    this.login1 = function(data){
        var me = this;
        deferred = $q.defer();
        $http({method: 'POST', url: 'sessions', data: data}).
        success(function(data, status, headers, config){
        deferred.resolve(data);
        }).
        error(function() {
            deferred.reject('1')
        });
        return deferred.promise;
    };

});

When controller use AuthService.login,then it will send two request.

For example:
This is no problem

POST /sessions HTTP/1.1
Host: 192.168.101.56:5000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0
Accept: application/json, text/plain, /
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
Referer: http://192.168.101.166:8888/
Content-Length: 28
Origin: http://192.168.101.166:8888
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

This is problem

OPTIONS /sessions HTTP/1.1
Host: 192.168.101.56:5000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Origin: http://192.168.101.166:8888
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

But, controller use AuthService.login1,then it will send one request,It's no problem.

Maybe,This problem because my view and controller is wrong,

This is my login view.

<form ng-submit="submit(username,password)"id="form-signin" class="form-signin" method="post">

    <section>
            <div class="input-group">
              <input type="text" class="form-control" ng-model='username' name="username" placeholder="Username">
              <div class="input-group-addon"><i class="fa fa-user"></i></div>
            </div>
            <div class="input-group">
              <input type="password" class="form-control" ng-model='password' name="password" placeholder="Password">
              <div class="input-group-addon"><i class="fa fa-key"></i></div>
            </div>
          </section>
          <section class="controls">
            <div class="checkbox check-transparent">
              <input type="checkbox" value="1" id="remember" name="remember" checked>
              <label for="remember">remember password</label>
            </div>
            <a href="#">forget password?</a>
          </section>
          <section class="log-in">
            <button type="submit" class="btn btn-greensea">login</button>
            <span>or</span>
            <a href="/registry" class="btn btn-slategray">register</a>
          </section>

</form>

This is my SessionCreateCtrl

Blog.controller('SessionCreateCtrl', function($scope, $location, Session, AuthService) {
    $scope.submit = function(user, passwd) {

        credentials={"email":user,"password":passwd}

        AuthService.login(credentials).then(function(user) {
            $location.path('/posts/create')
        }, function(response) {
            $scope.failedLoginAttempt = true;
        });
    }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant