0

I'm trying to get some data from this API, I got the XMLHttpRequest error even when I use this solution:

import 'package:http/http.dart' as http;

void main() async {
  
  var _headers = {
    "Access-Control-Allow-Origin": "*",
    'X-API-KEY': 'SqD712P3E82xnwOAEOkGd5JZH8s9wRR24TqNFzjk'
  };
  var url = "https://api.sunnah.com/v1/collections/muslim";

  var res = await http.get(Uri.parse(url), headers:_headers);
  print(res.body);
} 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
youcef.com
  • 42
  • 4

2 Answers2

0

It'll be working fine in production mode, but you can fix it in development mode as well, I have answered this one in an another stackoverflow post.

See my answer in the link below: Flutter Web Http Error : Uncaught (in promise) Error: XMLHttpRequest error

0

In my case, simply resolved by adding http://localhost:port (sender) to CORS_ALLOWED_ORIGINS in settings file.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34868151) – Ram Chander Aug 24 '23 at 14:06
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '23 at 08:09