【发布时间】:2015-09-13 21:41:26
【问题描述】:
我正在向 Tomcat 7 服务器发送 AJAX 请求,并且不断收到“错误请求”响应。我一直在使用 Chrome 和 FireFox 在 NetBeans 和 Eclipse 中进行调试。该请求似乎没有到达 Web 应用程序。我已经使用两种不同的 Web 工具捕获并验证了 JSON,并使用正在创建的请求对象仔细检查了所有变量名称。我是调试 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 也参与其中,控制器的摘录代码如下:
@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
}
【问题讨论】:
-
感觉和
Date lastUpdated字段有冲突! -
就是这样。我修改了 javascript 以从文本创建一个 Date 对象,它工作正常。感谢第二双眼睛。
-
对不起,我想提出一个详细的答案,但我的工作卡住了。很高兴它有帮助!
标签: javascript ajax json spring-mvc tomcat