I'm attempting to call an endpoint on my api, how every call I make goes to the error callback on the promise rather than the success callback, even if the request actually is a success (Returning 200 and valid json).
I'm using ngResource to make the call.
Resource:
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' }
}
});
}
In controller:
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)
gives:
{
"email": "bla6@bla.bla",
"password": "password",
"confirmPassword": "password"
}
as expected.
Next the console logs "error", so i know it's in the error callback. The next object that is logged is this:
{
"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": ""
}
Which doesn't really tell me anything about what went wrong.
Looking at the request in the network inspector in firefox tells me the error got a 200 response and returned the following json:
{
"message": "User was registered successfully"
}
I'm not quite sure else could go wrong.
This is my config if it's any help:
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:*/**"]);
}]);
And the request goes to: http://localhost:57053/api/Account/Register
as intended.
EDIT a bit more testing shows the same issue happens when using jQuery.post.
It's not any more descriptive on the error.
The code that runs on the api if it's any help.
// 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"}});
}
EDIT 2 This is the headers the call have.
Request headers:
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
Response headers:
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 request:
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 response:
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