jeudi 9 août 2018

Ionic issue with HttpClient.post (No 'Access-Control-Allow-Origin' header is present)

Well, I've tried to send a post request from HttpClient class in Ionic3, in the next code fragment I show the function in TypeScript.

constructor(public http: HttpClient, ...){ ... }

loginAndGetUserData(email0, password0): any
{
    return this.http.post
    (
        'http://xxx.a_api/api/login.json',
        {
            email: email0,
            password: password0
        },
        {
            headers:
            {
                'Access-Control-Allow-Origin':'*',
                'Access-Control-Allow-Methods':'POST, GET, OPTIONS, PUT',
                'Content-Type':'application/json',
                'Accept':'application/json'
            }
        }
    );
}

Then, when I call this method I receive an error with undefined code as response.

ErrorCode: undefined
Http failure response for (unknown url): 0 Unknown Error

Capture image of device
In Android logs, after the function call, I get this message:

08-09 21:16:31.649 19292-19292/io.ionic.starter D/SystemWebChromeClient: http://localhost:8080/: Line 0 : XMLHttpRequest cannot load http://xxx.a_api/api/login.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.
08-09 21:16:31.649 19292-19292/io.ionic.starter I/chromium: [INFO:CONSOLE(0)] "XMLHttpRequest cannot load http://xxx.a_api/api/login.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.", source: http://localhost:8080/ (0)

And in my PHP code I tried to put 'Access-Control-Allow-Origin:*' in header, but It receive empty data from the client. In some Ionic forum some people say that I have to modify ionic.config.json file putting proxies attribute with path and proxyUrl, but neither It resolve my issue.
proxies attribute, in ionic.config.json file, looks like:

"proxies":
[
    {
        "path": "/api",
        "proxyUrl": "http://xxx.a_api/api"
    }
]

Well, I don't know what to do, I'm sure that It's a client problem, 'cause the server receive empty data. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire