1

I am trying to do an $http.post and get the response, I tried in several ways but I can't get it working. The data I am sending (userData) is an json object.

 $http.post(url, userData)
    .success(function(data, status, headers, config) {
        console.log('post success');
        console.log('data');
        console.log(data);
        console.log('status');
        console.log(status);
        console.log('headers');
        console.log(headers);
        console.log('config');
        console.log(config);
    }).error(function(data, status, headers, config) {
        console.log('post error');
        console.log('data');
        console.log(data);
        console.log('status');
        console.log(status);
        console.log('headers');
        console.log(headers);
        console.log('config');
        console.log(config);
    });

And I am getting a 404 in status.

Thank you

5
  • Means requested URL does not exists. Give us more info so we can help you. Commented Mar 12, 2014 at 7:52
  • I am sure the URL exists. I make a wrong call like this <code> $http({ method: 'POST', url: 'url', data: userData, headers: {'Content-Type': 'application/x-www-form-urlencoded'} }) .success(function(data) { console.log('post success'); console.log('data'); console.log(data); }).error(function(data) { console.log('post error'); console.log('data'); console.log(data); }); <code> and the error is different is not 404, is 415 Unsupported Media Type, which is correct, because the userData is supposed to be json. Commented Mar 12, 2014 at 11:03
  • Without having more code I cannot give you any useful help. Either URL is not supporting POST method or some weird response from server. Commented Mar 12, 2014 at 13:39
  • Debugging with firebug I realized that the error in the console (when I show the status) is 404, but in the .Net is another error, 405 Method Not Allowed. I am running a server with nodejs (with express and cors) calling an external http service. Commented Mar 13, 2014 at 1:25
  • You are probably trying to get to a GET service via a POST request. Try to change $http.post to $http.get Commented Mar 13, 2014 at 10:04

2 Answers 2

1

The "405 Method not allowed" response is because your browser is doing a "preflight" OPTIONS method request via angular $http and your server doesn't support it. There are a bunch of prerequisite responses to enable CORS. If you have access to the server, you can add support for it. One option is to use nginx as a front-end proxy with this kind of configuration:

http://enable-cors.org/server_nginx.html

Read the comments--they are really informative.

Also: see "Preflighted Requests" here: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @Brian. The server doesn't support CORS, but I will try to figure out how to do it with nginx.
0

404 means that the URL you're sending the request to does not exist.

If you're using a sub-directory like mysite.com/angular-site, and you set the URL here to a relative path like "/url/to/api" - you will get to "mysite.com/url/to/path" instead of the correct "mysite.com/angular-site/url/to/path".

So pay attention to that, it might be the problem.

1 Comment

I am realizing that the hearders attributes are different when works, and when it doesn't. I am trying to change that with angularjs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.