0

I developing an application trough Ionic React.

The API is made with Laravel. I have the good data in the header of the response with 200 status.

Access-Control-Allow-Origin *
Access-Control-Allow-Credentials true
Access-Control-Allow-Methods GET, POST, PUT, DELETE, OPTIONS
Client side :

static getStat(): Promise<any>{
    return fetch(`http://192.168.1.250/bodt78/public/api/index`,{
        method: 'GET',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then((response) => {
        if(response.status === 200){
            return true
        }
        else{
            return false
        }
    })
    .catch(error => this.handleError(error));
}

This one works perfectly ... but when I want to make a request for the login ( POST )

return fetch(`http://192.168.1.250/bodt78/public/api/auth/login`,{
      method: 'POST',
      headers: { 'Content-Type': 'application/json',
                  'Access-Control-Allow-Origin': 'http://localhost:8100',
                },
      body: JSON.stringify({
      "email": email,
      "password": password}),
    }).then((res) => {
      console.log(res);
      if (res.status == 200) {
        return res.json();
      } else {
        if (res.status == 401) {
          return res.status;
        } else {
          return null;
        }
      }

    });

Every time I try to fetch ... I have an error :(

[Error] Origin http://localhost:8100 is not allowed by Access-Control-Allow-Origin.
[Error] Fetch API cannot load http://192.168.1.250/bodt78/public/api/auth/login due to access control checks.

I edit the ionic.config.json with :

  "proxies": [{
    "path": "/bodt78/public/api",
    "proxyUrl": "http://192.168.1.250"
}]

Do you have any idea ? I tried literally everything !

@sideshowbarker save your time and go to do something else rather than ban people just to improve the rank of your answer !

Thanks

Senthurkumaran
  • 1,738
  • 2
  • 19
  • 29
  • Does this answer your question? [Adding Access-Control-Allow-Origin header response in Laravel 5.3 Passport](https://stackoverflow.com/questions/39429462/adding-access-control-allow-origin-header-response-in-laravel-5-3-passport) – STA Jun 26 '20 at 05:32
  • 1
    Finally works with fruitcake/laravel-cors – bruno bruno Jun 27 '20 at 23:59

0 Answers0