2017-05-25 80 views
1

總的noobie問題,我剛剛開始使用Play框架。控制器POST操作響應「未授權」

我添加了一個動作「添加」到我的控制器,當我嘗試訪問它時,我得到了HTTP 403狀態的下一頁。

Unauthorized

控制器:

package controllers 

import javax.inject._ 

import play.api._ 
import play.api.data._ 
import play.api.data.Forms._ 
import play.api.mvc._ 

/** 
* This controller creates an `Action` to handle HTTP requests to the 
* application's home page. 
*/ 
@Singleton 
class HomeController @Inject() extends Controller { 

    val userForm = Form(
    mapping(
     "todo" -> text 
    )(TodoData.apply)(TodoData.unapply) 
) 

    /** 
    * Create an Action to render an HTML page. 
    * 
    * The configuration in the `routes` file means that this method 
    * will be called when the application receives a `GET` request with 
    * a path of `/`. 
    */ 
    def index = Action { implicit request => 
    Ok(views.html.index()) 
    } 

    def add = Action { implicit request => 
    Ok(views.html.index()) 
    } 
} 
case class TodoData(todo: String) 

路線

# An example controller showing a sample home page 
GET /       controllers.HomeController.index 
POST /add      controllers.HomeController.add 

# Map static resources from the /public folder to the /assets URL path 
GET  /assets/*file    controllers.Assets.versioned(path="/public", file: Asset) 

index.scala.html

@() 

@main("Todo App") { 
    <h1>Welcome to the Todo app</h1> 
    <form action="/add" method="post"> 
     <label for="todo">TODO:</label> 
     <input type="text" name="todo" id="todo"> 

     <button type="submit">Add</button> 
    </form> 
} 

回答

1

挖了幾個小時後,我周圍設法自己解決。一例RTFM。

似乎有一些魔術玩法與POST請求和CSRF令牌有關。我錯過了CSRF令牌。

我改變了形式的播放形式:

index.scala.html

@(form: Form[TodoData])(implicit request: RequestHeader, messages: Messages) 

@main("Todo App") { 
    <h1>Welcome to the Todo app</h1> 
    @helper.form(action = routes.HomeController.add()) { 
     @helper.CSRF.formField 
     @helper.inputText(form("todo")) 
     <button type="submit">Add</button> 
    } 
} 

控制器

package controllers 

import javax.inject._ 

import play.api.data._ 
import play.api.data.Forms._ 
import play.api.mvc._ 
import play.api.i18n._ 

/** 
* This controller creates an `Action` to handle HTTP requests to the 
* application's home page. 
*/ 
@Singleton 
class HomeController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport { 

    val todoForm = Form(
    mapping(
     "todo" -> text 
    )(TodoData.apply)(TodoData.unapply) 
) 

    /** 
    * Create an Action to render an HTML page. 
    * 
    * The configuration in the `routes` file means that this method 
    * will be called when the application receives a `GET` request with 
    * a path of `/`. 
    */ 
    def index = Action { implicit request => 
    Ok(views.html.index(todoForm)) 
    } 

    def add = Action { implicit request => 
    val errorFunction = { formWithErrors: Form[TodoData] => 
     // This is the bad case, where the form had validation errors. 
     // Let's show the user the form again, with the errors highlighted. 
     // Note how we pass the form with errors to the template. 
     BadRequest(views.html.index(formWithErrors)) 
    } 

    val successFunction = { data: TodoData => 
     // This is the good case, where the form was successfully parsed as a Data. 

     Redirect(routes.HomeController.index()).flashing("info" -> "Todo task added!") 
    } 

    val formValidationResult = todoForm.bindFromRequest 
    formValidationResult.fold(errorFunction, successFunction) 
    } 
} 

case class TodoData(todo: String) 
相關問題