Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top