Dart 如何修复向特定web服务器发出http请求时出现的颤振web http错误
我正在尝试在我的Flitter web应用程序中向000webhost发出HTTP请求,如下所示。第一种方法与第二种方法相同,我只更改了URL。然而,第一个有效,但第二个无效。有人建议添加更多的标题,但我不知道要添加哪些标题Dart 如何修复向特定web服务器发出http请求时出现的颤振web http错误,dart,flutter,flutter-web,hummingbird,Dart,Flutter,Flutter Web,Hummingbird,我正在尝试在我的Flitter web应用程序中向000webhost发出HTTP请求,如下所示。第一种方法与第二种方法相同,我只更改了URL。然而,第一个有效,但第二个无效。有人建议添加更多的标题,但我不知道要添加哪些标题 // This method WORKS getMethod()async{ print("IN GET ...."); String theUrl = 'https://jsonplaceholder.typicode.com/todos';
// 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上添加
标题add Access Control Allow Origin“*”
到.htaccess
,这为我解决了这个问题,你也可以像这样将下面的代码添加到你的php文件中:
<?php
require('connection.php');
header("Access-Control-Allow-Origin: *");
....
code goes here
....
?>
我在LocalHost上尝试了这一点,效果很好。当我使用flask作为服务器用户时,我使用cors npm包在节点或express服务器上实现了这一点。包的链接:。您可以通过以下操作将其用作中间件:const cors=require(“cors”);app.use(cors())问题的框架很好。在回答这个问题之前,我认为这是颤振软件包的问题。这个问题促使我在后端进行更改。