0

I had made a website in laravel.Now Iam making its mobile App using Ionic 3.

I am facing an following issue while making a API call to backend of laravel.

localhost/:1 XMLHttpRequest cannot load http://xyz.dev/request/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

Let say there are 12 routes defined in web.php of laravel.

Then in my project out of 12, 6 routes are for desktop web application and remaining 6 routes are prefixed with /mobile so that I can consume it in MY API. I am facing that issue while making any request through mobile app which is in IONIC and backend is in Larave.

Please Help me for this.

Mohammed
  • 319
  • 1
  • 3
  • 15
  • Possible duplicate of [XMLHttpRequest cannot load https://www.\[website\].com/](https://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-https-www-website-com) – Quentin Jun 23 '17 at 12:23

1 Answers1

0

You can add this in your laravel project through which you call apis

Put this in your filters.php file

Route::filter('alloworigin', function()
{   
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
    header('Access-Control-Allow-Credentials: true');
}); 

Through this you can successfully call cross platform api..

or you can also put this in your ajax call

$http({method: 'GET', url: 'http://localhost:3000/api/symbol/junk', 
            headers:{
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
                'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
                'X-Random-Shit':'123123123'
            }})
Hitarthi Panchal
  • 392
  • 2
  • 11