我想从客户端发送一个同步的帖子请求。根据文档,我们可以使用“异步”命名参数:
https://www.dartlang.org/articles/json-web-service/#saving-objects-on-the-server
var url = "http://127.0.0.1:8080/programming-languages";
request.open("POST", url, async: false);但是,上面的示例引发以下语法错误:
关键字“异步”、“等待”和“屈服”不能用作异步或生成器函数中的标识符。
如何发送同步的POST请求?
更新(5月27日,20:23)
我找到了解决这个问题的办法:
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;
}但是我不喜欢上面的解决方案,因为它看起来不够优雅。
发布于 2016-05-27 18:48:51
解决方案是使用postFormData()而不是send()。例如:
final req = await HttpRequest
.postFormData(url, {'action': 'delete', 'id': id});
return req.responseText;发布于 2016-05-27 18:16:20
这是这个命名参数https://github.com/dart-lang/sdk/issues/24637的已知问题。
发布于 2017-05-23 04:53:49
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;}
这是重点,有时你需要“放”或“删除”
https://stackoverflow.com/questions/37489623
复制相似问题