gpt4 book ai didi

java - Play Framework 2.0 json - 接收空 JsonNode 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:29:43 26 4
gpt4 key购买 nike

我在以下代码中得到了一个空的 JsonNode 对象。客户端 javascript 有问题。如果我使用 POST 请求运行 curl 命令,我会得到正确的响应。如果我在下面提到的 Javascript 中运行相同的 - 我得到 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>

消息.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

有人可以帮助确定 javascript 文件中的错误吗。

最佳答案

ajax 调用的预期结果是一个 json 对象。在这种情况下,如果您想用纯文本进行响应,请将 dataType: 'text' 添加到请求中:

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

关于java - Play Framework 2.0 json - 接收空 JsonNode 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15969441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com