gpt4 book ai didi

javascript - 尝试使用 AJAX 发布 JSON 并接收 400

转载 作者:行者123 更新时间:2023-11-28 23:30:23 25 4
gpt4 key购买 nike

我正在向 Tomcat 7 服务器发送 AJAX 请求,但一直收到“错误请求”响应。我一直在使用 Chrome 和 FireFox 在 NetBeans 和 Eclipse 中进行调试。该请求似乎没有到达 Web 应用程序。我使用两种不同的 Web 工具捕获并验证了 JSON,并使用正在创建的 Request 对象仔细检查了所有变量名称。我是调试 Tomcat 的新手,非常感谢任何帮助。我的 JavaScript 代码如下:

function submitScoring(selected, dataSource) {
if (!validate()) {
alert("Please complete the highlighted fields.");
return false;
}

var documents = getDocuments($("#table-available").dataTable(), selected);

var request = {
"database": dataSource,
"userName": $("#userName").val(),
"modelName": $("#modelName").val(),
"documents": documents
};

sendRequest(request, "scoring");
}

function getDocuments(srcTable, areSelected) {
var documents = new Array();

if (areSelected) {
srcTable.$("tr.selected").each(function () {
documents.push(getDocument(this));
});
} else {
srcTable.$("tr").each(function () {
documents.push(getDocument(this));
});
}
return documents;
}

function getDocument(row) {
return {
"metadata": {
"id": row.cells[0].textContent,
"title": row.cells[1].textContent,
"language": row.cells[2].textContent,
"format": row.cells[3].textContent
},
"lastUpdated": row.cells[4].textContent,
"documentUri": row.cells[5].textContent
};
}

function sendRequest(request, type) {
var json = JSON.stringify(request);

$.ajax({
url: $appRoot + "/submit/" + type,
dataType: "json",
type: "POST",
contentType: "application/json",
data: json,
success: function (data, textStatus) {
alert("Your " + type + " job request was submitted successfully.");
},
error: function (xhr, textStatus, errorThrown) {
alert('request failed ' + errorThrown);
}
});
}

编辑:还涉及 Spring MVC, Controller 的摘录代码如下:

@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "/scoring", method = RequestMethod.POST)
public @ResponseBody Map<String, String> submitDocsToScore(@RequestBody ScoringRequest jsonRequest, HttpServletRequest request, HttpServletResponse response)
throws InvalidFormatException, AccumuloException, AccumuloSecurityException, IOException, TableNotFoundException, Docx4JException,
ShutdownSignalException, ConsumerCancelledException, InterruptedException, org.json.simple.parser.ParseException {

ralInterface.makeRequest(jsonRequest);

return new HashMap<String, String>();
}

编辑 2:我之前说过我已经仔细检查过了,但让我在这里也发布相关代码:

public abstract class Request { 
protected String database;
protected String userName;
protected String modelName;
// getters and setters follow
}

public class ScoringRequest extends Request {
private List<Document> documents;
// getters and setters follow
}

public class Document implements Serializable {
private Map<String, String> metadata;
private String documentUri;
private Date lastUpdated;
// getters and setters follow
}

最佳答案

正如 Sudhansu Choudhary 所指出的,当从 getDocument 函数返回时,我需要使用实际的日期对象而不是字符串。

关于javascript - 尝试使用 AJAX 发布 JSON 并接收 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31081584/

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