首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dart:同步邮寄请求

Dart:同步邮寄请求
EN

Stack Overflow用户
提问于 2016-05-27 17:53:16
回答 3查看 2.2K关注 0票数 0

我想从客户端发送一个同步的帖子请求。根据文档,我们可以使用“异步”命名参数:

https://www.dartlang.org/articles/json-web-service/#saving-objects-on-the-server

代码语言:javascript
复制
var url = "http://127.0.0.1:8080/programming-languages";
request.open("POST", url, async: false);

但是,上面的示例引发以下语法错误:

关键字“异步”、“等待”和“屈服”不能用作异步或生成器函数中的标识符。

如何发送同步的POST请求?

更新(5月27日,20:23)

我找到了解决这个问题的办法:

代码语言:javascript
复制
Future<String> deleteItem(String id) async {
    final req = new HttpRequest()
      ..open('POST', 'server/controller.php')
      ..send({'action': 'delete', 'id': id});
    // wait until the request have been completed
    await req.onLoadEnd.first;
    // oh yes
    return req.responseText;
}

但是我不喜欢上面的解决方案,因为它看起来不够优雅。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-27 18:48:51

解决方案是使用postFormData()而不是send()。例如:

代码语言:javascript
复制
final req = await HttpRequest
    .postFormData(url, {'action': 'delete', 'id': id});
return req.responseText;
票数 0
EN

Stack Overflow用户

发布于 2016-05-27 18:16:20

这是这个命名参数https://github.com/dart-lang/sdk/issues/24637的已知问题。

票数 0
EN

Stack Overflow用户

发布于 2017-05-23 04:53:49

代码语言:javascript
复制
Future<String> deleteItem(String id) async {
final req = new HttpRequest()
  ..open('POST', 'server/controller.php')
  ..send({'action': 'delete', 'id': id});
// wait until the request have been completed
await req.onLoadEnd.first;
// oh yes
return req.responseText;

}

这是重点,有时你需要“放”或“删除”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37489623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档