2

new to Angular and javascript. I am using Angular to connect to a service on a different server. I am able to get data just fine, but when I am trying to post, I get a 404. I tried to follow the Angular documentation as closely as possible. What am I doing wrong?

PS. I have sniffed my traffic, and I noticed that my POST hex was completely empty. I am stumped.

EDIT: Heres the error(s) I am getting in the console.

OPTIONS http://url 404 (Not Found) angular.js:7997<br>
OPTIONS http://url Invalid HTTP status code 404 angular.js:7997<br>
XMLHttpRequest cannot load http://url. Invalid HTTP status code 404



app.controller('SendController', ['$scope', 'Request', function($scope, Request) {

    $scope.request = {
        // test data here
    };

    $scope.send = function() {
        Request.save($scope.request);
    };
}]);


app.factory('Request', ['$resource',
    function($resource) {
        return $resource(url);
    }]);
justin.c
  • 193
  • 2
  • 2
  • 10
  • You need to share your server side code as well - the 404 means the server can't find the route. – Ed_ Mar 10 '14 at 17:30
  • Hey Ed, thanks for the response. I don't have access to the server side code. If it helps any, I am able to use Postman, and am able to post to my url just fine. – justin.c Mar 10 '14 at 17:51
  • `url` at the line `return $resource(url);` is not defined anywhere – Beterraba Mar 10 '14 at 18:08
  • Thanks for the response Beterraba. It is defined in a global variable inside my app. I just shortened it for visuals. I can confirm that it is working, because I can see the full url in the console when the error returns. – justin.c Mar 10 '14 at 18:13
  • You can get a response via `curl` from that url? – Beterraba Mar 10 '14 at 18:24
  • @Beterraba Yep, I tried that. I got data back. – justin.c Mar 10 '14 at 19:16
  • Your console error actually shows `http://url`? – Beterraba Mar 10 '14 at 19:24
  • @Beterraba I replaced my real url with "url". I can see the full url in the console. – justin.c Mar 10 '14 at 19:52
  • I've never used `$resource` (I always tend to use the lower level `$http` methods, but are you sure there is enough code there? The console output you have shown seems to suggest the request is an `OPTIONS` request and not a `POST`..? – Ed_ Mar 11 '14 at 09:58

1 Answers1

6

It looks like it was something on the server side configuration. We had set the Access-Control-Allow-Origin to *. According to the answer below, Chrome no longer supports that.

Original (wrong):

header("Access-Control-Allow-Headers: *");

Corrected:

header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

For more information check out this post. https://stackoverflow.com/a/18192705/3010896

Community
  • 1
  • 1
justin.c
  • 193
  • 2
  • 2
  • 10