2017-03-06 3 views
1

HTMLページからJSONをPlayサーバーに送信しています。サーバーコードは奇妙な形で動作しています。デバッガを使用して、私はサーバーが最初に悪い要求応答を送信し、次に私のコントローラメッセージを処理し、Okに応答することがわかります。OKを送信していますが、ブラウザに不正なリクエストが表示されています

画像1では、画像にJSONのスナップショットがあり、直ちに画像3に示されているリクエストレスポンスが悪くなります。画像2では、コントローラコードが実行され(サーバが不正なリクエストを送信した後に仮定します)、サーバは別の200 OKを送信します。 JQueryで使用しているコンテンツタイプはtext/jsonです。 application/jsonを使用すると、リクエストメッセージが不正になり、コントローラコードがまったく実行されない

問題がJsonにあると思われます。

@(form:Form[User2]) 

<!DOCTYPE html> 
<html lang="en" xmlns:font-variant="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="UTF-8"> 
    <meta http-equiv="X-UA-COMPATIBLE" content="IE=edge"> 
    <meta name="viewport" content="width=device-width,initial-scale=1"> 
    <title>HTML Test</title> 
    <link rel="stylesheet" type="text/css" href="@routes.Assets.at("stylesheets/bootstrap.min.css")"> 

    <!--link rel="stylesheet" href="stylesheets/bootstrap-theme.min.css"--> 
    <style type="text/css"> 

     html, body { 
      height:100%; 
      margin:0;padding:0 
     } 

     .center-form { 
      width:100%; 
      margin:auto; 
      position:relative; 
      top:50%; 
      transform: translateY(-50%) 
} 


</style> 

    <script src="@routes.Assets.at("javascripts/jquery-3.1.1.min.js")"></script> 
    <script src="@routes.Assets.at("javascripts/bootstrap.min.js")"></script> 


</head> 
<body> 

<div class="container center-form" > 

    <!-- for medium and large screens, 
    First row of Bootstrap grid contains logo. Total 3 columns (12/4). Logo in middle column--> 

    <div class="row" > 
     <!--empty column--> 
     <div class="col-md-4 col-lg-4" ></div> 

     <!--logo column--> 
     <!--div class="col-md-4 col-lg-4" > 
      <div> 
       <img src="@routes.Assets.at("images/SalesWorkspaceLogo303x64.png")" alt="SalesWorkspace Logo" height="64" width="303"> 
      </div> 
     </div--> 
     <!--empty column--> 
     <div class="col-md-4 col-lg-4"></div> 
     </div> 

    <!-- for medium and large screens, 
    Second row of Bootstrap grid contains the form for username and password. Total 3 columns (12/4). --> 
     <div class="row" > 
      <!--empty column--> 
      <div class="col-md-4 col-lg-4"></div> 

      <!--form--> 
      <div class="col-md-4 col-lg-4"> 

      <form id="registration-form" action="/newreg" method="post"> 
        <div class="form-group"> 
         <label for="first-name">First Name</label> 
         <input type="text" class="form-control" id="first-name" name="first-name" value="@form("name").value" required> 
        </div> 

        <div class="form-group"> 
         <label for="last-name">Last Name</label> 
         <input type="text" class="form-control" id="last-name" name="last-name" value="@form("name").value" required> 
        </div> 

        <div class="form-group"> 
         <label for="email">Email</label> 
         <input type="email" class="form-control" id="email" name="email" value="@form("email").value" required> 
        </div> 
        <div class="form-group"> 
         <label for="password">Password</label> 
         <input type="password" class="form-control" id="password" name="password" required> 
        </div> 
        <div class="form-group"> 
         <label for="confirm-password">Confirm password</label> 
         <input type="password" class="form-control" id="confirm-password" required> 
        </div> 

        <div class="form-group"> 
         <label for="street-name">Street Name</label> 
         <input type="text" class="form-control" id="street-name" name="street-name" required> 
        </div> 

        <div class="form-group"> 
         <label for="country">Country</label> 
         <input type="text" class="form-control" id="country" name="country" required> 
        </div> 

        <button type="submit" class="btn btn-primary">Sign Up</button> 
       </form> 
      </div> 
      <!--empty column--> 
      <div class="col-md-4 col-lg-4"></div> 
     </div> 
</div> 
<script src="@routes.Assets.at("javascripts/myScripts.js")"></script> 
</body> 
</html> 

コントローラ

def registrationRequest = Action (parse.json){ request => 
    (request.body \ "first-name").asOpt[String].map {name => 
      Ok("hello " + name) 
    }.getOrElse{ 
     BadRequest("bad request") 
    } 
    } 
} 

myScripts.js

function objectifyForm(formArray) {//serialize data function 

    returnArray = {}; 
    for (var i = 0; i < formArray.length; i++){ 
    returnArray[formArray[i]['name']] = formArray[i]['value']; 
    } 
    return returnArray; 
} 

$(document).ready(function(){ 
    // click on form submit 
    $('#registration-form').on('submit',function(e){ 
    var details = JSON.stringify(objectifyForm($(this).serializeArray())); 
    console.log(details) 
     // send ajax 
     $.ajax({ 
      url: '/newreg', // url where to submit the request 
      type : "POST", // type of action POST || GET 
      datatype : "json", // data type 
      /* this doesn't work*/ 
      //contentType: "application/json; charset=utf-8", 
      /*this does*/ 
      contentType: "text/json; charset=utf-8", 
      data : details, 
      success : function(result) { 
       // you can see the result from the console 
       // tab of the developer tools 
       console.log(result); 
      }, 
      error: function(xhr, resp, text) { 
       console.log(xhr, resp, text); 
      } 
     }) 
    }); 
}); 

enter image description here

enter image description here

enter image description here

答えて

1

元の送信イベントをキャンセルすることを忘れてしまい、フォームを2回送信します。簡単なHTML形式の1回限りで、プレイは悪い要求を返す。もう一度JSONとしてjavascriptで処理して、それを処理して200を返します。

JS関数に1つの文字列を追加するだけです。

から:

$('#registration-form').on('submit',function(e){ 

へ:

$('#registration-form').on('submit',function(e){ 
    e.preventDefault(); //prevent form from default submitting 
関連する問題