Angularjs承诺即使在成功的情况下总是会进行错误回调

时间:2015-12-30 19:58:11

标签: javascript angularjs asp.net-web-api2

我正在尝试在我的api上调用一个端点,即使请求实际上是成功的(返回200和有效的json),我所做的每个调用都会转到promise的错误回调而不是成功回调。

我正在使用ngResource拨打电话。

资源:

angular.module("ya").factory("Account", [
    "$resource", function ($resource) {
        return $resource(apiUrl, {}, {
            "register": {
                method: "POST",
                params: {},
                url: apiUrl + "api/Account/Register"
            },
            "login": {
                method: "POST",
                params: {},
                url: apiUrl + "token",
                headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
            }
        });
    }

在控制器中:

var creds = {
    email: register.email,
    password: register.password,
    confirmPassword: register.password
}
var r = Account.register(creds);
console.log(r);
r.$promise.then(function(result) {
    console.log(result);
}, function(result) {
    console.log("error");
    console.log(result);
});

console.log(r)给出:

{
    "email": "bla6@bla.bla",
    "password": "password",
    "confirmPassword": "password"
}

正如所料。

接下来控制台记录“错误”,所以我知道它在错误回调中。 记录的下一个对象是:

{
    "data": null,
    "status": -1,
    "config": {
        "method": "POST",
        "transformRequest": [null],
        "transformResponse": [null],
        "url": "http://localhost:57053/api/Account/Register",
        "data": {
            "email": "bla6@bla.bla",
            "password": "password",
            "confirmPassword": "password"
        },
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json;charset=utf-8"
        }
    },
    "statusText": ""
}

这并没有真正告诉我什么是错的。

在firefox中查看网络检查器中的请求告诉我错误得到了200响应并返回了以下json:

{
    "message": "User was registered successfully"
}

我不太确定其他方法可能出错。

如果有任何帮助,这是我的配置:

angular.module("ya", ["ngMaterial", "ui.router", "ngResource"])
    .config(["$mdThemingProvider", "$stateProvider", "$urlRouterProvider", "$locationProvider", "$sceDelegateProvider", 
    function ($mdThemingProvider, $stateProvider, $urlRouterProvider, $locationProvider, $sceDelegateProvider) {

        // Set application theme
        $mdThemingProvider.theme("default").dark();

        // Enable html5 mode
        $locationProvider.html5Mode(true);

        // Set default page
        $urlRouterProvider.otherwise("home");

        $stateProvider.state("home", {
            url: "/home",
            templateUrl: "templates/home.html",
            controller: "homeController", 
            controllerAs: "home"
        }).state("auth", {
            url: "/auth",
            templateUrl: "templates/authentication.html",
            controller: "authController",
            controllerAs: "auth"
        });

        $sceDelegateProvider.resourceUrlWhitelist(["self", "http://*.zlepper.dk/**", "http://localhost:*/**"]);
    }]);

请求按预期转到:http://localhost:57053/api/Account/Register

编辑更多测试显示使用jQuery.post时会出现同样的问题。

对错误没有任何描述性。

如果有任何帮助,在api上运行的代码。

    // POST api/Account/Register
    [AllowAnonymous]
    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        return Ok(new Dictionary<string, string>() {{"message", "User was registered successfully"}});
    }

编辑2 这是通话的标头。

请求标题:

Host: localhost:57053
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://localhost:3000/auth
Content-Type: application/json;charset=utf-8
Content-Length: 76
Origin: http://localhost:3000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

回复标题:

Cache-Control: no-cache
Content-Length: 53
Content-Type: application/json; charset=utf-8
Date: Wed, 30 Dec 2015 21:15:28 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcUmFzbXVzXERvY3VtZW50c1xHaXRIdWJcY3VybHktZ2FyYmFuem9cY3VybHktZ2FyYmFuem9cYXBpXEFjY291bnRcUmVnaXN0ZXI=?=
access-control-allow-credentials: true
access-control-allow-origin: http://localhost:3000, *

OPTIONS请求:

OPTIONS /api/Account/Register HTTP/1.1
Host: localhost:57053
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: http://localhost:3000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

OPTIONS响应:

HTTP/1.1 200 OK
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcUmFzbXVzXERvY3VtZW50c1xHaXRIdWJcY3VybHktZ2FyYmFuem9cY3VybHktZ2FyYmFuem9cYXBpXEFjY291bnRcUmVnaXN0ZXI=?=
X-Powered-By: ASP.NET
Date: Thu, 31 Dec 2015 10:29:07 GMT
Content-Length: 0

0 个答案:

没有答案