gpt4 book ai didi

java - 使用 JQuery 和 Java 在 Play Framework 中构建自动保存功能,但遇到 400 Bad Request 或 404 Not Found 错误

转载 作者:行者123 更新时间:2023-12-02 11:46:20 28 4
gpt4 key购买 nike

我正在我的 Play 应用程序中使用模型中的 JQuery 构建自动保存功能,并在 Controller 中处理请求。

我最初发布(第 1 篇)这个:

Play Framework JQuery AJAX REST Post call returns Bad Request error

但这被标记为可能与此重复(第 2 篇文章):

play framework routes trouble (400 bad request)

我的原始帖子(帖子 1)没有收到任何答案,因此我被指示重新发布我的问题。

我按照帖子 2 删除了我传递的参数,因为基于帖子 2,这可能是我的请求的问题。帖子 2 中引用的文档位于 Scala 中(对于旧版本的 Play - 我正在使用2.5)而我的应用程序是用Java编写的,所以它并没有真正帮助我。我在 Play 文档中找到了这个:

https://www.playframework.com/documentation/2.5.x/JavaJsonActions

我阅读并遵循该文档并更新了我的代码。

但是,我仍然收到错误请求 (400) 或未找到 (404) 错误。我没有收到任何详细消息,所以我不确定我在哪里犯了错误。

以下是 Chrome 控制台中返回的内容:

addptp:414 POST http://localhost:9000/autosave 404 (Not Found)
send @ jquery-3.2.1.min.js:4
ajax @ jquery-3.2.1.min.js:4
(anonymous) @ addptp:414
each @ jquery-3.2.1.min.js:2
each @ jquery-3.2.1.min.js:2
autosave @ addptp:413
(anonymous) @ addptp:185

这是 View 中的 JQuery:

var timer;
var restURL = window.location.protocol + "//" + window.location.hostname + (window.location.port == "" ? "" : (":" + window.location.port));

$(document).ready(function() {
// JSON REST Autosave...
timer = setInterval(function() {
autosave();
}, 6000);
});

function autosave() {
alert("Autosave");
$('form').each(function() {
$.ajax({
type: "POST",
url: restURL + "/autosave",
data: $(this).serialize(),
dataType: "json",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success: function(data) {
alert("Success!");
},
error: function(data) {
alert("Error!");
}
});
});
}

这是我的 Java Controller 代码:

@BodyParser.Of(BodyParser.FormUrlEncoded.class)
public Result restAutosaveAdultPTP() {
// Let's get the current request in JSON and parse...
Map<String, String[]> json = request().body().asFormUrlEncoded();
if (json == null) {
return badRequest("Expecting Json data");
} else {
return ok("json: " + json);
}
}

这是我的routes 文件条目:

POST   /autosave   controllers.Application.restAutosaveAdultPTP()

这是请求和响应信息...

一般:

Request URL:http://localhost:9000/autosave
Request Method:POST
Status Code:404 Not Found
Remote Address:[::1]:9000
Referrer Policy:no-referrer-when-downgrade

响应 header :

HTTP/1.1 404 Not Found
Content-Length: 26917
Content-Type: text/html; charset=utf-8
Date: Thu, 25 Jan 2018 18:34:15 GMT

请求 header :

POST /autosave HTTP/1.1
Host: localhost:9000
Connection: keep-alive
Content-Length: 2739
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:9000
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:9000/addptp
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: PLAY_SESSION=a37c3087c9d5d3f4129057c964ca9a90eef80fe5-SESSION_ID=kfs7q8vohk3ammlfcnfka0j1p9&email=dzeller44%40gmail.com&userTimeout=1516880046558

有效负载

legacy_Provider_Id=&new_Provider_Id=&provider_Name=&alternate_Name_DBA=&setting_Type=&waivers_Served=&total_Number_Served=&total_Number_on_Waivers=&street_Address=&apartment=&city=&state=&zip=&phone=&email1=&email2=&contact_Person=&updates=&main_PTP_Address=&duplicate_docs=&other_Rights_NonCompliance_Description=&other_Remedy_for_Rights_Description=&details_for_Rights_Action_Plan=&remedy_Rights_Incurred_Cost_Description=&remedy_Rights_One_Time_Costs=&remedy_Rights_Recurring_Annual_Costs=&person_for_Rights_Action_Plan=&status_Rights_Action_Plan=---+Select+Status+---&other_Choice_NonCompliance_Description=&other_Remedy_for_Choice_Description=&detail_for_Choice_Action_Plan=&remedy_Choice_Incurred_Cost_Description=&remedy_Choice_One_Time_Costs=&remedy_Choice_Recurring_Annual_Costs=&person_for_Choice_Action_Plan=&status_of_Choice_Action_Plan=---+Select+Status+---&other_Integration_NonCompliance_Description=&other_Remedy_for_Integration_Description=&detail_for_Integration_Action_Plan=&remedy_Integration_Incurred_Cost_Description=&remedy_Integration_One_Time_Costs=&remedy_Integration_Recurring_Annual_Costs=&person_for_Integration_Action_Plan=&status_for_Integration_Action_Plan=---+Select+Status+---&other_Remedy_for_Institutional_Factors_Description=&detail_for_Institutional_Action_Plan=&remedy_Institutional_Incurred_Cost_Description=&remedy_Institutional_One_Time_Costs=&remedy_Institutional_Recurring_Annual_Costs=&person_for_Institutional_Action_Plan=&status_of_Institutional_Action_Plan=---+Select+Status+---&ptp_Edited_Date=&ptp_Status_Notes=&update_Due=&compliance_Status=---+Select+Status+---&ensures_Rights=---+Select+Status+---&optimizes_Autonomy=---+Select+Status+---&selected_by_Person=---+Select+Status+---&options_in_PC_Plan=---+Select+Status+---&facilitates_Choice=---+Select+Status+---&integrated_in_Community=---+Select+Status+---&enforceable_Lease=---+Select+Status+---&eviction_Protections=---+Select+Status+---&bedroom_Privacy=---+Select+Status+---&bedroom_Locks=---+Select+Status+---&choice_of_Roommates=---+Select+Status+---&freedom_to_Decorate=---+Select+Status+---&free_Schedule_Access_to_Food=---+Select+Status+---&visitors_any_Time=---+Select+Status+---&physically_Accessible=---+Select+Status+---&modifications_PC_Plan=---+Select+Status+---&setting_NonInstitutional=---+Select+Status+---&heightened_Scrutiny_Description=&cdphe_Lead=&site_Visit_Status=---+Select+Status+---&site_Visit_Date=&site_Visit_Team=&documents_Detail=&findings_from_Documents=&summary_of_Findings=&promising_Practices=&additional_Notes=&follow_up_Visit_Status=---+Select+Status+---&additional_Notes_2=&follow_up_Date=&follow_up_Team=&documents_Detail_2=&findings_from_Documents_2=&summary_of_Findings_2=&promising_Practices_2=

正如下面的评论所述,为什么它在请求 header 中查找 json 而不是表单 URL 编码:

Accept: application/json, text/javascript, */*; q=0.01

感谢您提供的任何指导或帮助。

最佳答案

问题是 $(this).serialize() 正在以 URL 编码表示法对数据进行编码,但 @BodyParser.Of(BodyParser.Json.class) Controller 中的注释和 request().body().asJson() 代码表明您需要 JSON 编码。

序列化文档:https://api.jquery.com/serialize/

The .serialize() method creates a text string in standard URL-encoded notation.

您可能不想使用 JSON 的 serialize 方法,而是使用 JavaScript 的 stringify 之类的方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

The JSON.stringify() method converts a JavaScript value to a JSON string

或者,您可以更改 Play 代码以获取 URL 编码值。请参阅此处有关 FormUrlEncoded 的文档:https://www.playframework.com/documentation/2.6.x/JavaBodyParsers

FormUrlEncoded: Parses the body as a form.

如果您更改 Play 行为,请记住同时更改您发送的 contentType,使其为 application/x-www-form-urlencoded 而不是 application/json.

关于java - 使用 JQuery 和 Java 在 Play Framework 中构建自动保存功能,但遇到 400 Bad Request 或 404 Not Found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176578/

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