Error making a post request in angular using application / json

2

Previously I made a request using the following in the Headers

'Content-Type': 'application/x-www-form-urlencoded'

But now the API asks me to send it in the following way:

'Content-Type': 'application/json'

The function that performs the request is as follows:

function Authentication (data) {

    var url = 'http://url'; 

    return $http.post(url, $httpParamSerializer(datos), {
        headers: {
            'Content-Type': 'application/json'
        }
    });

};

Which gives me the following error

Error

OPTIONS http://API_URL 405 (Method Not Allowed)

 XMLHttpRequest cannot load http://API_URL. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.9:8100' is therefore not allowed access. The response had HTTP status code 405.

But when I use a tool, in my case ARC (Advance Rest Client) works. Which can be seen in the image

The API is hosted on an Azure server.

    
asked by Pedro Miguel Pimienta Morales 28.02.2017 в 22:25
source

3 answers

1

It's a CORS problem as you have been told.

Although the solution is to "touch" the backend to be solvent, you can also try an extension for the browser (It has worked for me in some cases only).

In Chrome for example I use: Allow-Control-Origin

And I have it set so that in Intercepted URLs or URL patterns I have added the value: *: // *

You can try to see if that solves you at least to get out of step until they tell you how to correctly modify the back part.

    
answered by 30.03.2017 в 11:40
0

To see brother, try the following:

var url = 'http://url'; 

var parametros = JSON.stringify({username:user_email, password:user_password});    

return $http.post(url, parametros, {
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        }
    });

Good luck: D

    
answered by 01.03.2017 в 02:31
0

This is a CORS problem ( link ). In short, when you make an Ajax call from the browser, the browser first makes an OPTIONS request, to negotiate with the server if the request can be made. When the origin and the destination are the same server, there is no problem, but when they are different, the server must allow said origin to make the request.

When the server answers, the Access-Control-Allow-Origin header must contain your IP (or the ip of the angularjs application).

For this case, you must configure your server (where you have exposed the REST services) so that in the Access-Control-Allow-Origin header it has something like Access-Control-Allow-Origin="*". At least that's what I did in java.

Greetings

    
answered by 06.03.2017 в 22:04