Home > Back-end >  Not fetching data from website and giving XMLHttpRequest error
Not fetching data from website and giving XMLHttpRequest error

Time:05-06

so I got this "XMLHttpRequest" error and I've been trying to fix it for a few hours but got nowhere with it... this is my code:

var response = await http.get(Uri.https('sigarra.up.pt','feup/pt/cantina.ementashow#7'));
    var jsonData = jsonDecode(response.body);
    print(jsonData);

I have more code but at the moment I'm just trying to see if it gets the info from the website, which it clearly isn't doing.

Can someone please point me in the right direction? Thanks alot :)

CodePudding user response:

import 'package:wnetworking/wnetworking.dart';

void main(List<String> arguments) async {
  var raw = await HttpReqService.get<String>(
    'https://sigarra.up.pt/feup/pt/cantina.ementashow#7',
    jsonResponse: false,
  );
  print(raw?.substring(0,200));
}

Output:

<!DOCTYPE html>
<html lang="pt" dir="ltr">
<head>
<link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png" /><link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-1

CodePudding user response:

If you are getting a response of type xml and using jsonDecode() to format it, you will get errors. If instead you are getting a response of type json try to add 'Content-Type': 'application/json' to your request headers. Eg:

http.get(uri, headers: {'Content-Type': 'application/json'}) 
  • Related