英文:
How to send @RequestParam in AJAX POST request
问题
以下是您的代码的翻译部分:
我有一个接受请求参数的 API:
@PostMapping(value = "/export")
@ResponseBody
public ResponseEntity<String> bulkExport(
@RequestParam(value = "managedObjects", required = false) List<String> managedObjects) {
// 数据
}
我想发送 AJAX POST 请求:
$.ajax({
type: "POST",
//url: "policy/js_policy",
url: "/export/",
async: false,
data: { "managedObjects": ["Audit","Logs"] },
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
// 文件处理
}
});
我尝试在URL中发送managedObjects
,在数据中也发送了相同的参数,但我的API没有工作。如何确切地从AJAX POST请求中发送@RequestParam
参数?
英文:
I have my API which accepts Request Param:
@PostMapping(value = "/export")
@ResponseBody
public ResponseEntity<String> bulkExport(
@RequestParam(value = "managedObjects", required = false) List<String> managedObjects) {
//data
}
);
I want to send AJAX POST request.
$.ajax({
type: "POST",
//url: "policy/js_policy",
url: "/export/ ,
async: false,
data: { "managedObjects": ["Audit","Logs"]},
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
//File Handling
}
});
I tried to send managedObjects in URL. In data also I am sending the same.But my API is not working. How to send the @RequestParam from AJAX POST request exactly?
答案1
得分: 3
在"Query Param"中传递一个列表
$.ajax({
...
url: "/export?managedObjects=Audit,Logs" ,
...
});
在"Request Body"中传递一个列表
$.ajax({
type: "POST",
url: "/export/",
...
data: {managedObjects[0]: "Audit",
managedObjects[1]: "Logs"}
...
});
英文:
pass a list in Query Param
$.ajax({
...
url: "/export?managedObjects=Audit,Logs" ,
...
});
pass a list in Request Body
$.ajax({
type: "POST",
url: "/export/",
...
data: {managedObjects[0]: "Audit",
managedObjects[1]: "Logs"}
...
});
答案2
得分: 0
Here's the translated code portion:
尝试将您的数据转化为字符串:
var data = {
managedObjects: ["Audit", "Logs"]
}
$.ajax({
type: "POST",
url: "/export/",
async: false,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
}
});
此外,您应该在@RequestParam中使用"name"而不是"value":
@PostMapping(value = "/export")
@ResponseBody
public ResponseEntity<String> bulkExport(
@RequestParam(name = "managedObjects", required = false) List<String> managedObjects) {
//data
}
请注意,我只翻译了您提供的代码部分,没有其他内容。
英文:
Try stringifying your data:
var data = {
managedObjects: ["Audit", "Logs"]
}
$.ajax({
type: "POST",
url: "/export/",
async: false,
data: JSON.Stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
}
});
Additionally you should use "name" instead "value" in @RequestParam:
@PostMapping(value = "/export")
@ResponseBody
public ResponseEntity<String> bulkExport(
@RequestParam(name = "managedObjects", required = false) List<String> managedObjects) {
//data
}
);
答案3
得分: 0
I think the problem is just with the list that you want to send in your request.
var dataToSend = {
list: [{ fieldname: 'ABC' }, { fieldname: 'DEF' }]; // your list should be something like this.
$.ajax({
type: "POST",
//url: "policy/js_policy",
url: "/export/?managedObjects=" + Mos,
async: false,
data: JSON.stringify(dataToSend),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
}
});
(Note: The code you provided contains some syntax issues. You may need to fix those to make it work correctly.)
英文:
I think the problem is just with list that you want to send in your request.
var dataToSend = {
list: [{ fieldname: 'ABC' }, { fieldname: 'DEF' }]; // your list should something like this.
$.ajax({
type: "POST",
//url: "policy/js_policy",
url: "/export/?managedObjects=" + Mos ,
async: false,
data: JSON.stringify(dataToSend),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (XMLHttpRequest, textStatus) {
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论