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);
} 
2
  • Cannot reproduce Your issue, it works as expected, see screenshot: pasteboard.co/WNDnnbgYESJf.jpg Commented Mar 11, 2022 at 20:50
  • Agree with @num8er, this code works fine for me. Commented Mar 11, 2022 at 20:51

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.