Ionic - Send post from localhost to PHP external API

0

Hello

I'm developing an app with Cordova / Ionic and I'm trying to send data via post like this:

public async login(usuario: string, senha: string): Promise<any[]>{
    let body = new FormData();
    body.append('usuario', usuario);
    body.append('senha', senha);

    let response: any = await this.http
        .post(this.api.url + '?model=usuario&function=login', body, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                'Token': this.api.token
            }
        })
        .toPromise()
        .catch(error => console.log(error));

    console.dir(response);

    if(response == null){
        response = [];
    }

    return response;
}

I happen to be at localhost and the api at an external php site. I have been able to make requests via get, but the post does not work, api does not receive any data.

To get around cross origin problems I made a proxys definition in the ionic.config.json file.

When I run the function that performs POST, I can see four requests being sent using the chrome debug:

Request URL: link Request Method: GET Status Code: 301 Moved Permanently

Request URL: link Request Method: POST Status Code: 301 Moved Permanently

Request URL: link Request Method: OPTIONS Status Code: 200 OK

Request URL: link Request Method: GET Status Code: 200 OK

...

It seems like where it would be to send a POST ends up sending a GET.

    
asked by anonymous 05.11.2018 / 17:07

1 answer

0

I have solved the problem. They were two things:

1 - In the file ionic.config.json I put the wrong proxyUrl address, was missing the 'www' (for some reason making GET requests worked even without the 'www')

2 - I removed the 'Content-Type' from the 'Header' of the request and thus I was able to receive data sent via post in array format in PHP.

    
06.11.2018 / 18:15