我正在学习Dart,并且正在使用forcemvc软件包。我的表单呈现,但是当我单击按钮时,出现以下错误:/ verify不允许使用POST方法。
这是我的主要功能:
library mongomvc1;
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
import 'dart:html';
import 'package:forcemvc/force_mvc.dart';
part 'controllers/postcontroller.dart';
void main() {
// querySelector('#output').text = 'Your Dart app is running.';
WebApplication app = new WebApplication();
app.host = "127.0.0.1";
app.port = 8080;
app.use("/", (req, model) => "index");
app.use("/verify", (req, model) => "verify", method: "POST");
app.start();
}
这是我的表格:
<form id="form" action="/verify" method="POST">
<p>First name: <input type="text" id="first" name="first" required /></p>
<p>Last name: <input type="text" id="last" name="last" required /></p>
<p><button type="submit">Continue</button>
</form>
这是我的POST函数:
part of mongomvc1;
@Controller
class PostController {
@RequestMapping(value: "/verify", method: "POST")
Future postMethod(req, Model model) async {
req.GetPostParams().then((map) {
model.addAttribute('first', map['first']);
model.addAttribute('last', map['last']);
req.Async(null);
});
model.addAttribute('status', 'ok');
return req.AsyncFuture;
}
}
我究竟做错了什么?
最佳答案
我测试了您的代码,您需要执行以下操作:
library mongomvc1;
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'package:forcemvc/force_mvc.dart';
part 'controllers/postcontroller.dart';
void main() {
WebApplication app = new WebApplication();
app.host = "127.0.0.1";
app.port = 8080;
app.use("/", (req, model) => "index");
// app.use("/verify", (req, model) => "verify", method: "POST");
print("started");
app.start();
}
/ verify已被postcontroller.dart使用,因此在这种情况下,您需要将其放在注释中或删除该行!
part of mongomvc1;
@Controller
class PostController {
@RequestMapping(value: "/verify", method: "POST")
Future postMethod(ForceRequest req, Model model) async {
req.getPostParams().then((map) {
model.addAttribute('first', map['first']);
model.addAttribute('last', map['last']);
req.async(null);
});
model.addAttribute('status', 'ok');
return req.asyncFuture;
}
}
您在req上的所有方法都必须小写而不是大写。
使用新的异步/等待,您还可以按以下方式编写代码:
part of mongomvc1;
@Controller
class PostController {
@RequestMapping(value: "/verify", method: "POST")
Future postMethod(req, Model model) async {
var map = await req.getPostParams();
model.addAttribute('first', map['first']);
model.addAttribute('last', map['last']);
model.addAttribute('status', 'ok');
}
}
通过这些调整,该职位应该可以工作。如果您还有其他疑问,请询问!
关于forms - Dart不允许我使用POST方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990361/