本文介绍了如何向Flutter Web中发出HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在颤抖的网络上动手.我尝试使用http包将我创建的简单Flutter Web应用程序连接到mysql数据库和localhost.但是我没有从请求方法返回任何数据.当我尝试打印出 snaphot.error
时,我得到了: XMLHttpRequest error
.我在 FutureBuilder()
I was trying my hands on flutter web. I tried to connect a simple flutter web app I created to mysql database and localhost using the http package. However I dont get any data returned from the request method. When I tried to print out snaphot.error
I got this: XMLHttpRequest error
. I have this method in a FutureBuilder()
getMethod()async{
String theUrl = 'https://localhost/fetchData.php';
var res = await http.get(Uri.encodeFull(theUrl),headers: {"Accept":"application/json"});
var responsBody = json.decode(res.body);
print(responsBody);
return responsBody;
}
推荐答案
您还可以像下面这样将以下代码添加到您的php文件中:
You can also Add the code below to your php file like so:
<?php
require('connection.php');
header("Access-Control-Allow-Origin: *");
....
code goes here
....
?>
我在LocalHost上进行了尝试,并且有效
I Tried this on LocalHost and it worked
这篇关于如何向Flutter Web中发出HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!