我想在 dart 中进行 http 调用,但是当我尝试时,有些网站会返回 ProgressEvent 错误,有些则不会。我不明白。
import 'dart:html';
void main() async {
print(await HttpRequest.getString('https://swapi.co/api/people/1'));
}
Run Code Online (Sandbox Code Playgroud)
我期望的输出
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "https://swapi.co/api/planets/1/",
"films":[
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
"species":[
"https://swapi.co/api/species/1/"
],
"vehicles":[
"https://swapi.co/api/vehicles/14/",
"https://swapi.co/api/vehicles/30/"
],
"starships":[
"https://swapi.co/api/starships/12/",
"https://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://swapi.co/api/people/1/"
}
Run Code Online (Sandbox Code Playgroud)
但回报是
Uncaught Error: Instance of 'ProgressEvent'
Run Code Online (Sandbox Code Playgroud)
在我的测试中,它取决于 url:如果我尝试使用相同的代码,但 url 是:https://jsonplaceholder.typicode.com/posts,则 json 已成功加载。 示例 json 正确返回
小智 1
您是否尝试过设置允许 CORS?我的 REST 服务器也有同样的问题,我允许 CORS 来源并且它有效!您可以使用以下站点: https: //www.test-cors.org/ 使用此页面来测试 CORS 请求。
| 归档时间: |
|
| 查看次数: |
2289 次 |
| 最近记录: |