Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable CORS while an XMLHttpRequest error occurs in flutter web

when I will add data to the database using the function, and on the server I have added Access-Control-Allow-Origin so that it is not blocked by CORS, but still error when I looked in the browser console tools tab console

Access to XMLHttpRequest at 'https://int.goo.id/api/pg/sso.register' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

how to deactivate CORS??

like image 970
OceanL Avatar asked Sep 03 '19 04:09

OceanL


People also ask

What is Xmlhttprequest error in flutter Web?

This error is a Javascript error and it can be inspected from AndroidStudio terminal or from the browser console. If you run into this problem it means that the requests to the API server are failing due to a CORS error.


2 Answers

For anyone who is using node.js server, enable cors support in your server side code.

  1. npm i cors express
  2. In index.js or server.js -
const express = require("express");

const app = express();
var cors = require("cors");
app.use(cors()); 

This should fix cors issue with flutter web

like image 165
Captain_Satya Avatar answered Sep 19 '22 11:09

Captain_Satya


For enabling cors on my server and to resolve XMLHttpRequest error in flutter web

I am using this in my .htaccess file for allowing access to certain domains only

 <ifModule mod_headers.c>
    SetEnvIf Origin "http(s)?://(localhost:25120|domain.com|domain2.com)$" AccessControlAllowOrigin=$0
    Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
    Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
 </ifModule>    
like image 42
Vicky Salunkhe Avatar answered Sep 22 '22 11:09

Vicky Salunkhe