$ http帖子被取消了

时间:2015-04-24 12:34:55

标签: javascript angularjs angularjs-http

我有一个登录表单,我试图向服务器发送一个帖子请求,但它被取消了。

这是控制器:

angular.module('app.signin', [])
.controller('SigninController', ['$http', function($http) 
{
    this.email      = null;
    this.password   = null;

    this.signIn = function()
    {
        var res = $http.post( 'http://api.svcassist.dev/index.php/auth/login', {email: this.email, password: this.password });

        res.success(function(data, status, headers, config) 
        {
                alert(data);
        });
    };
}]);

这里是html:

<div class="container" style="max-width:300px" ng-controller="SigninController as signin">
    <form class="form-signin">
        <h2 class="form-signin-heading">Sign In</h2>
        <label for="inputEmail" class="sr-only">Email address</label>
        <input type="email" id="inputEmail" name="email" class="form-control" placeholder="Email address" required autofocus ng-model="signin.email">
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required ng-model="signin.password">
        <br/>
        <button class="btn btn-lg btn-primary btn-block" ng-click="signin.signIn()">Sign In</button>
    </form>
</div> 

并且Chrome表示请求已取消,以下是响应标题:

Request URL:http://api.svcassist.dev/index.php/auth/login
Request Headers
Provisional headers are shown
Accept:application/json, text/plain, */*
Content-Type:application/json;charset=UTF-8
Origin:http://localhost:8000
Referer:http://localhost:8000/app/index.html
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:6C7F2AA6-481A-32ED-0CFC-8F4B9F5927B8
Request Payloadview parsed
{"email":"email","password":"pass"}

1 个答案:

答案 0 :(得分:0)

我建议使用ng-submit:

HTML

<div class="container" style="max-width:300px" ng-controller="SigninController as signin">
    <form ng-submit="signin.signIn(signin.email, signin.email)" class="form-signin">
        <h2 class="form-signin-heading">Sign In</h2>
        <label for="inputEmail" class="sr-only">Email address</label>
        <input type="email" id="inputEmail" name="email" class="form-control" placeholder="Email address" required autofocus ng-model="signin.email">
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required ng-model="signin.password">
        <br/>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign In</button>
    </form>
</div> 

JS

this.signIn = function(email, password)
{
    var res = $http.post( 'http://api.svcassist.dev/index.php/auth/login', {email: email, password: password });

    res.success(function(data, status, headers, config) 
    {
            alert(data);
    });
};