We're having problems using POST via ng-resource in Angular 1.0.5 with Yeoman scaffolding. We're running frontend on a localhost and using Azure hosted asp.net web api. We have done the CORS handling in web api, and manage to POST, but still recieve Allow-Cross-Origin error. And we can see the data in the database. This is followed by a GET error.
So the question becomes: Would you believe this to be a frontend problem, or backend problem. Some code snippets for explanation:
The service
angular.module('jellybeanApp.Service', ['ngResource'])
.factory('PeopleServices', function ($http, $rootScope, $resource, $routeParams, $location) {
return {
users : function() {
var ret = $resource('http://verkvaki.azurewebsites.net/api/user/:UserId',
{callback: 'JSON_CALLBACK'},
{
get: {method: 'JSONP', params: {UserId: $routeParams.UserId}},
query: {method: 'JSONP'},
create: {method: 'POST'}
});
return ret;
}
}
});
The controller
'use strict';
angular.module('jellybeanApp')
.controller('PeopleCtrl', function ($scope, PeopleServices) {
$scope.userResult = PeopleServices.users().query();
})
.controller('EditPeopleController', function ($scope, PeopleServices, $routeParams) {
var UserId = $routeParams.UserId;
$scope.result = PeopleServices.users().get();
})
.controller('CreatePeopleCtrl', function ($scope, PeopleServices, $location) {
$scope.newUser = {
Username : 'Notendanafn',
Password : 'Lykilorð',
FirstName : 'Fornafn',
LastName : 'Eftirnafn',
Ssn : 'Kennitala',
Email : 'Tölvupóstfang',
Phone : 'Símanúmer',
Roles : 'Stjornandi',
JobTitle : 'Starfstitill',
IsActive : true
};
$scope.savePeople = function (data) {
PeopleServices.postit(data);
};
app.js
'use strict';
var app = angular.module('jellybeanApp', ['jellybeanApp.Service'])
.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'main'
})
.when('/people/new', {
controller: 'CreatePeopleCtrl',
templateUrl: '../views/newPeople.html'
})
.otherwise({
redirectTo: '/'
});
});
After POST this happens to URL bar : http://localhost:9000/people/people
where the should be only http://localhost:9000/people
And this error in Chrome console : GET http://verkvaki.azurewebsites.net/api/user/people?callback=angular.callbacks._2 400 (Bad Request)
The route to POST in the API should only be /user, so the people attribute there surprises aswell.
And finally, the URL to the azure hosted API : http://verkvaki.azurewebsites.net/api/user
Any thoughts?