如何解决向特定Web服务器发出HTTP请求的Flutter Web HTTP错误 [英] how to fix flutter web http error making http request to a particular web server
问题描述
我试图在下面的Flutter Web应用程序中向000webhost发出HTTP请求.第一种方法与第二种方法相同,只是更改了URL.但是,第一个有效,但是第二个无效.有人建议添加更多标题,但我不知道要添加哪些标题.
I am trying to make an HTTP request 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 adding more headers but I have no idea which headers to add.
// 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;
}
// This DOES NOT WORK
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;
}
推荐答案
经过一段时间的努力,我发现问题出在服务器端我必须在000webhost上的 .htaccess
上添加 Header并添加Access-Control-Allow-Origin"*"
,这为我解决了
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
这篇关于如何解决向特定Web服务器发出HTTP请求的Flutter Web HTTP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!