JavaScript CORS Issue with React app and Laravel API cors issue with localhost,cors issue with post request,cors

I have a React app which is at http://localhost:3000/ and Laravel API is at http://localhost/blog/public/api/
I get the following error

Access to fetch at 'http://localhost/blog/public/api/auth/signin' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Error Screenshot

Here are the response headers :-

Response Headers Screenshot

I tried via htaccess, https://packagist.org/packages/barryvdh/laravel-cors

Answer:1

Create a new middleware

‘Php artisan make:middleware cors’

Put this in the created middle to replace the handle method

    public function handle($request, Closure $next) {

    return $next($request)
      ->header('Access-Control-Allow-Origin', '*')
      ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
      ->header('Access-Control-Allow-Headers',' Origin, Content-Type, Accept, Authorization, X-Request-With')
      ->header('Access-Control-Allow-Credentials',' true');
}

Then go to Kernel.php file and add this under the The application's global HTTP middleware stack.

protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\Cors::class,//cors added here 
 ];

Enjoy!

Answer:2

In Laravel to access API without CORS error then you need to add CORS PKG in your Laravel Project.

https://github.com/barryvdh/laravel-cors

You can use this to fix this error.

Answer:3

I have some basic html below, and for some reason it's not recognizing the js file I linked in the script tag. Am I linking it incorrectly in some way?: <html> <head> <link rel="...

I have some basic html below, and for some reason it's not recognizing the js file I linked in the script tag. Am I linking it incorrectly in some way?: <html> <head> <link rel="...

  1. html isnan
  2. html isn't a programming language
  3. html isn't done
  4. html isnull
  5. html isnumber
  6. html isnumeric
  7. html insert image
  8. html isn't working
  9. html isn't centered
  10. what is html
  11. isnan html javascript
  12. html image isn't showing up
  13. html page isn't scrolling
  14. html string isnullorempty
  15. html link isn't working
  16. angular html isnan
  17. html javascript isnumeric
  18. html picture isn't showing
  19. is_numeric php html
  20. why isn't html a programming language

I understand that 2000 options in a select box is going to bring along some performance issues, but it works fine on Chrome and Firefox and Safari. Basically, I am calling a web service which ...

I understand that 2000 options in a select box is going to bring along some performance issues, but it works fine on Chrome and Firefox and Safari. Basically, I am calling a web service which ...

  1. angularjs select with search
  2. angularjs select with ng-repeat
  3. angularjs select with ng-options
  4. angularjs select with checkbox
  5. angularjs select with default option
  6. angularjs select with search filter
  7. angularjs select with filter
  8. angularjs select with input
  9. angularjs select with empty option
  10. angularjs select with autocomplete
  11. angularjs select with default value
  12. angularjs select with search example
  13. angularjs select with options
  14. angularjs select with placeholder
  15. angularjs select with images
  16. angularjs select with ng-model
  17. angularjs select with object
  18. angularjs select with ng-change
  19. angularjs select with typeahead
  20. angularjs select with static options

I learn JavaScript now, and encounter with next problem: I have page with some frames, and I want to load some page into one of specified frames; But code below does not do what I want. Could you ...

I learn JavaScript now, and encounter with next problem: I have page with some frames, and I want to load some page into one of specified frames; But code below does not do what I want. Could you ...

  1. open window framed art
  2. open window frame clipart
  3. window open frame target
  4. window.open frameless
  5. window.open frame javascript
  6. window open framename
  7. openframeworks window
  8. open window picture frame
  9. open wooden window frame
  10. open in parent window/frame
  11. chrome open frameless window
  12. window.open hidden frame
  13. html open frameless window
  14. javascript open frameless window
  15. window open new frame
  16. javascript window open frameset
  17. open new window robot framework
  18. window.open blocked a frame with origin
  19. window.open in same frame
  20. wpf open window inside frame

I have several cases on my software where I have an array of observables and I need to execute them in order. Having the next subscription to happen only after the previous is complete. So Im using ...

I have several cases on my software where I have an array of observables and I need to execute them in order. Having the next subscription to happen only after the previous is complete. So Im using ...

  1. concat observable array
  2. merge array observable
  3. rxjs concat array of observables
  4. angular concat array of observables