2013-04-12 60 views
3

我在下面的代碼中得到一個空的JsonNode對象。客戶端JavaScript有問題。如果我使用POST請求運行curl命令,我會得到正確的響應。如果我在下面提到的Javascript中運行相同的代碼 - 我爲JsonNode對象得到一個空值。下面的代碼玩框架2.0 json - 接收null JsonNode對象

Application.java 
package controllers; 

import org.codehaus.jackson.JsonNode; 

import play.Routes; 
import play.mvc.Controller; 
import play.mvc.Result; 
import views.html.index; 


public class Application extends Controller { 

    public static Result index() { 
     return ok(index.render("Your new application is ready.")); 
    } 

    public static Result receiveData() { 
     response().setContentType("application/json"); 
     JsonNode json = request().body().asJson(); 
     if(json == null) { 
     return badRequest("Expecting Json data"); 
     } else { 
     String name = json.findPath("name").getTextValue(); 
     if(name == null) { 
      return badRequest("Missing parameter [name]"); 
     } else { 
      return ok("Hello " + name); 
     } 
     } 
    } 

    public static Result javascriptRoutes() { 
     response().setContentType("text/javascript"); 
     return ok(
     Routes.javascriptRouter("jsRoutes", 
      // Routes 
      controllers.routes.javascript.Application.receiveData() 
     ) 
    ); 
    } 

} 


Routes 
# This file defines all application routes (Higher priority routes first) 
# ~~~~ 

# Home page 
GET /       controllers.Application.index() 
POST /receiveData    controllers.Application.receiveData() 
GET /assets/javascripts/routes controllers.Application.javascriptRoutes() 

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

main.scala.html

@(title: String)(content: Html) 
@import helper._ 
@import controllers.routes.javascript._ 

<!DOCTYPE html> 

<html> 
    <head> 
     <title>@title</title> 
     <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"> 
     <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/bootstrap.css")"> 
     <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")"> 
     <script type="text/javascript" src="@routes.Application.javascriptRoutes"></script> 
     <script src="@routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"></script> 
     <script src="@routes.Assets.at("javascripts/bootstrap.js")" type="text/javascript"></script> 
     <script src="@routes.Assets.at("javascripts/message.js")" type="text/javascript"></script> 

    </head> 
    <body> 
     <div class="container"> 
      <div class = "row"> 
      <div class = "span12"> 
       <input type = "text" id = "inputValue" value="getData"></input> 
       <button class = "approveButton btn btn-primary">Approve </button> 
      </div> 
      </div> 
     </div> 

    </body> 
</html> 

message.js

$(document).ready(function(){ 
    $('.approveButton'). click(function(){ 
     var value = $('#inputValue').val(); 
     var jsonToSend = {}; 
     jsonToSend.name = value; 
     var outputData = JSON.stringify(jsonToSend); 
     jsRoutes.controllers.Application.receiveData().ajax({ 
     data:outputData, 
     contentType:'application/json', 
     success: function(data) { 
      console.log(data); 
     }, 
     error: function() { 
      alert("Error!"); 
     } 
     }); 
    }); 
    }); 

這裏的curl命令的輸出:

curl --header "Content-type:application/json" --request POST --data '{"name":"abx"}' http://localhost:9000/receiveData 
Hello abx 

可有人請幫助識別jav中的錯誤ascript文件。

回答

2

ajax調用的預期結果是一個json對象。如果您要回應純文本添加的dataType這種情況下:「文本」請求:

jsRoutes.controllers.Application.receiveData().ajax({ 
     data:outputData, 
     dataType: 'text', 
     contentType:'application/json', 
     success: function(data) { 
      console.log(data); 
     }, 
     error: function() { 
      alert("Error!"); 
     } 
     }); 
+0

我用POST嘗試很好,但同樣的結果。 –

+0

好吧,現在我看到了問題。我編輯答案 – TizianoPiccardi

+0

將類型設置爲文本工作。謝謝。 –

0

也許一個愚蠢的問題,但你怎麼想,Ajax調用將POST而不是GET?你有沒有嘗試通過GET取代POST?