gpt4 book ai didi

dart - 如何修复向特定 Web 服务器发出 http 请求的 flutter web http 错误

转载 作者:行者123 更新时间:2023-12-03 03:26:09 26 4
gpt4 key购买 nike

我正尝试在我的 flutter web 应用程序中向 000webhost 发出 HTTP 请求,如下所示。第一种方法和第二种一样,我只是改了URL。但是,第一个有效,但第二个无效。有人建议添加更多 header ,但我不知道要添加哪些 header 。

// 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 上添加 Header add Access-Control-Allow-Origin "*".htaccess 这为我解决了这个问题

关于dart - 如何修复向特定 Web 服务器发出 http 请求的 flutter web http 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56209073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com