Future<void> _getShopListAllJson() async {
try {
final response = await http.get("http://abair.gq/db_dept_info_all.php");
if (response.statusCode == 200) {
print(response.statusCode);
print(response.body);
setState(() {
_data = jsonDecode(response.body) as List;
});
} else {
print("Some error: ${response.statusCode}");
}
} catch (e) {
print(e);
}
}请参阅上面的代码 flutter web api 调用错误 xmlhttprequest
Error
Code
最佳答案
看起来 CORS 正在阻止它,请尝试在您的 URL 前面添加一个 CORS 代理。
将此添加到您的 URL https://cors-anywhere.herokuapp.com/ 前
final response = await http.get("https://cors-anywhere.herokuapp.com/http://abair.gq/db_dept_info_all.php");
一旦您确认它正在工作,最好拥有自己的代理,因此使用以下方法创建一个:https://github.com/Rob--W/cors-anywhere/#documentation
关于Flutter web api 调用 XMLHttpRequest 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62572828/