forms - Dart不允许我使用POST方法

标签 forms dart dart-force-mvc

我正在学习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/

相关文章:

mongodb - 在ForceFrameworkMvc中使用对象

forms - 重用单个 Angular 5 react 形式元素

ruby-on-rails - Ruby on Rails 多个图像连接到一个对象

javascript - 提交表单后重定向页面 - Javascript

java - j2me中的canvas和form有什么区别?

dart - 运行发布安装。请确保 Git 已正确安装。苹果操作系统

dart - Dart 1 书籍是否仍然与学习 Dart 2 相关

flutter - 如何从 GraphQL 生成 Dart - graphql 到 dart 生成器