skip to Main Content

I am using flutter to create a web application, I got this error while accessing the API I don’t know what is going wrong I am using .NET for backend which is connected with SAP for data. Can someone tell me how to fix this:-

Access to XMLHttpRequest at ‘API_URL’
from origin ‘http://localhost:64848’ 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.

Here is my code :-

await DioUtil.getInstance()
      .post(
    apiUrl,
    options: Options(
      headers: {'content-type': 'application/json', 'Access-Control-Allow-Origin': true, "Accept": "*/*"},
    ),
    data: body,
  )
      .then((value) {
    
      debugPrint("Status Code == " + value.statusCode.toString());
      
      } else {
        tryAgainDialog(getContractFullReport);
        setState(() {
          _isLoading = false;
        });
      }
    });
  });

I am using interceptor with basic auth for authorization to backend.

2

Answers


  1. You can use this run command:
    flutter run -d chrome –web-browser-flag "–disable-web-security"

    If you are using vscode, you can add this arg to your launch.json:
    {"name": , "request": "launch", "type": "dart", "args": ["–web-browser-flag=–disable-web-security"]}

    Login or Signup to reply.
  2. You will need to configure CORS properly on your server side.

    Since you did not post any code, I can only suggest you look at Microsofts Documentation how to do this. This is the latest, if you are using older version, they are available, too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search