Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Guru5005's avatar

Consuming Laravel API from Different domain ( front end )

Hi, I need assistance or help for this issue, been searching from 4 days but could not figure it out. I have created API's in laravel and i am using Passport for Authentication. I am consuming the API's from different domain (react with fetch API).

All the GET requests are working , by giving 200 ok code, but when i make a post request to log the user in i get no response.

If i make login request from Post Man App , its working. Token is getting issued after login.

What's the issue?

Any help would be appreciated.

My fetch API POST request

        let formData = new FormData();
            formData.append('email', email);
            formData.append('password', password);
            fetch('http://localhost:8000/api/login', {
                        mode: 'no-cors',
                        body : formData,
                       // crossDomain: true,
                       //dataType: 'json',
                        method: 'POST',
                headers: {
                    "Accept": "application/json",
                    "X-Requested-With" : 'XMLHttpRequest'
                },
                    }
            ).then(response => {
                if (response.ok) {
                response.json()
                        .then(json => {
                    console.log(json);

            });
            }
        });
0 likes
9 replies
Guru5005's avatar

I have Already added that from composer and added class to the middle ware group in kernel.php, i tried all the solutions, now i am feeling that it might be problem due to the browser, i am using firefox v54.0.

I am getting following error in console

TypeError: NetworkError when attempting to fetch resource.

lostdreamer_nl's avatar

can you make a screenshot of the failng request? Is there an OPTIONS request right before it?

1 like
lostdreamer_nl's avatar

ok, You should really be doing that OPTIONS request, but you have disabled it in your client JS:

mode: 'no-cors',

that wont magically make everything work. CORS is build into your browser, telling your browser not to do the cors request (OPTIONS) will just break your app as you just found out.

You really need the server to send you the header:

Access-Control-Allow-Origin 
1 like
Guru5005's avatar

@LOSTDREAMER_NL - if i remove mode : no-cors , Even GET request is not working.

Can u suggest me any example to consume own API built with Laravel API.

Thank you for the reply

lostdreamer_nl's avatar
Level 53

if you remove the no-cors, you will see that, for every request, there is another extra OPTIONS request (which currently will fail, so the GET request wont even go out)

After you are at that point, you need to change the API code to accept requests coming from external domains, you do this by using CORS headers.

Install this package in your API: https://github.com/barryvdh/laravel-cors

That will make sure your server will respond with the correct headers, after you have done that (and have removed that mode: 'no-cors' from your js code) you should be getting 2 requests for every API call, first an OPTION request, and then the actual GET / POST request.

1 like
Guru5005's avatar

Any example related to this ?? working example using axios or fetch or ajax method???

Just 1 example would be sufficient to get started

Please or to participate in this conversation.