如何修复Flutter Web HTTP错误,使HTTP请求发送到特定的Web服务器 [英] how to fix flutter web http error making http request to a particalar web server

查看:199
本文介绍了如何修复Flutter Web HTTP错误,使HTTP请求发送到特定的Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Flutter Web应用程序中将http requset设置为000webhost,如下所示。第一种方法与第二种方法相同,只是更改了网址。但是第一个有效,但是第二个无效。有人建议添加更多标头,但我不知道要添加哪些标头。

I am trying to make an http requset to 000webhost as below in my flutter web app. The first method is the same as the second,I only changed the url. However the first one works but the second does not work. someone suggested to add more headers but I have no idea which headers to add.

////此方法有效
getMethod()async {

/// This method WORKS getMethod()async{

  print("IN GET ....");
  String theUrl = 'https://jsonplaceholder.typicode.com/todos';
  var res = await http.Client().get(Uri.encodeFull(theUrl),headers: {"Accept":"application/json"});
  //var res = await http.get(Uri.encodeFull(theUrl),headers: {"Accept":"application/json"});
  var responsBody = json.decode(res.body);
  print(responsBody);
  return responsBody;

}

////这不起作用

getMethod()async{

  print("IN GET ....");
  String theUrl = 'https://funholidayshotels.000webhostapp.com/fetchData.php';
  var res = await http.Client().get(Uri.encodeFull(theUrl),headers: {"Accept":"application/json"});
  //var res = await http.get(Uri.encodeFull(theUrl),headers: {"Accept":"application/json"});
  var responsBody = json.decode(res.body);
  print(responsBody);
  return responsBody;

}


推荐答案

一段时间以来,我发现问题出在服务器端
我必须添加 Header添加Access-Control-Allow-Origin * .htaccess 在000webhost上,这为我解决了

After struggling with this for some time I figured out the problem was from the server side I had to add Header add Access-Control-Allow-Origin "*" to .htaccess on 000webhost And This solves it for me

这篇关于如何修复Flutter Web HTTP错误,使HTTP请求发送到特定的Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆