gpt4 book ai didi

javascript - Java 类接收 Ajax JSON post 作为 null

转载 作者:行者123 更新时间:2023-12-02 09:36:01 25 4
gpt4 key购买 nike

我正在尝试使用ajax post方法将json数据发送到Java类。当我硬编码出一个 json 字符串时,我的 Java 对象接收它很好,但是当我尝试发送使用 JSON.stringify() 创建的 json 字符串时,我在 Controller 中得到所有空值...

这是流程,我有以下 html 表格:

<table class="table" id="internalUnsubmittedPartRequests">
<thead>
<tr>
<th id="partID">partID</th>
<th id="quantity">quantity</th>
<th id="applicableModel">applicableModel</th>
<th id="queueName">queueName</th>
<th id="issueType">issueType</th>
<th id="controlCode">controlCode</th>
<th id="position">position</th>
<th id="command">command</th>
<th id="activationDate">activationDate</th>
<th id="flushDate">flushDate</th>
</tr>
</thead>
<tbody>
<!-- Dynamically Generated via newRequest.js -->
</tbody>
</table>

下面的 javascript 从该表中读取并使用 ajax 将其发送到我的 Controller :

$('#submit_set_btn').click(function(event) {
var myRows = [];
var $headers = $("th");
var $rows = $("#internalUnsubmittedPartRequests tr").each(function(index) {
$cells = $(this).find("td");
myRows[index] = {};
$cells.each(function(cellIndex) {
myRows[index][$($headers[cellIndex]).html()] = $(this).html();
});
});
myRows.shift();
var myObj = {};
myObj.myrows = myRows;
console.log(JSON.stringify(myObj));

$.ajax({
type : "POST",
// contentType : "application/json; charset=utf-8",
url : window.location,
data : JSON.stringify(myObj),
dataType : 'json',
success : function(result) {
//alert("success");
console.log("success");
console.log(result);
},
error : function(e) {
//alert("fail");
console.log("fail");
console.log(e);
}
});
});

在 console.log(JSON.stringify(myObj)) 调用期间,以下内容将打印到控制台:

{"myrows":[{"partID":"data","quantity":"data","applicableModel":"data","queueName":"data","issueType":"data","controlCode":"data","position":"data","command":"data","activationDate":"data","flushDate":"data"}]}

这是代表我尝试创建的对象的 Java 类:

public class NewPartRequest
{
private String partID;
private String quantity;
private String applicableModel;
private String queueName;
private String issueType;
private String controlCode;
private String position;
private String command;
private String activationDate;
private String flushDate;

@Override
public String toString() {
return getPartID() + " " + getQuantity() + " " + getApplicableModel() + " " +
getQueueName() + " " + getIssueType() + " " + getCommand() + " " +
getActivationDate() + " " + getFlushDate();
}


//Getters and Setters below this line

最后 Controller 类接收 JSON 数据并使用 toString 方法显示我们得到的内容:

@Controller
public class DistributionNewRequestController {

@RequestMapping(value = "/distribution/internal/unsubmitted/new-request", method = { RequestMethod.GET })
public String newRequest() {
return "new-request";
}

@RequestMapping(value = "/distribution/internal/unsubmitted/new-request", method = {
RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody String internalRequestPost(@RequestBody NewPartRequest newPartRequest) {

System.out.println("The new part request toString method: ");
System.out.println(newPartRequest.toString());

String response = jsonify(newPartRequest);
return response;

}

private String jsonify(NewPartRequest in) {
Gson gson = new Gson();
String out = gson.toJson(in);
return out;
}

}

在 NewPartRequest newPartRequest 对象上调用 toString() 方法,并打印以下内容:

The new part request toString method: 
null null null null null null null null

我已经被这个问题困扰很长时间了,我不明白为什么我的所有属性的值都是空。

最佳答案

您需要更改 ajax 帖子,如下所示

data : JSON.stringify(myObj.myrows[0]),

或者您可以像这样更改后端代码

List<NewPartRequest>

然后像下面这样从ajax传递

data : JSON.stringify(myObj.myrows),

您实际上传递了一个对象列表,并期望后端 Controller 方法中只有一个对象。

关于javascript - Java 类接收 Ajax JSON post 作为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57516326/

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