如何在AJAX POST请求中发送@RequestParam

huangapple go评论129阅读模式
英文:

How to send @RequestParam in AJAX POST request

问题

以下是您的代码的翻译部分:

  1. 我有一个接受请求参数的 API
  2. @PostMapping(value = "/export")
  3. @ResponseBody
  4. public ResponseEntity<String> bulkExport(
  5. @RequestParam(value = "managedObjects", required = false) List<String> managedObjects) {
  6. // 数据
  7. }
  1. 我想发送 AJAX POST 请求
  2. $.ajax({
  3. type: "POST",
  4. //url: "policy/js_policy",
  5. url: "/export/",
  6. async: false,
  7. data: { "managedObjects": ["Audit","Logs"] },
  8. contentType: "application/json; charset=utf-8",
  9. dataType: "json",
  10. complete: function (XMLHttpRequest, textStatus) {
  11. // 文件处理
  12. }
  13. });

我尝试在URL中发送managedObjects,在数据中也发送了相同的参数,但我的API没有工作。如何确切地从AJAX POST请求中发送@RequestParam 参数?

英文:

I have my API which accepts Request Param:

  1. @PostMapping(value = &quot;/export&quot;)
  2. @ResponseBody
  3. public ResponseEntity&lt;String&gt; bulkExport(
  4. @RequestParam(value = &quot;managedObjects&quot;, required = false) List&lt;String&gt; managedObjects) {
  5. //data
  6. }
  7. );

I want to send AJAX POST request.

  1. $.ajax({
  2. type: &quot;POST&quot;,
  3. //url: &quot;policy/js_policy&quot;,
  4. url: &quot;/export/ ,
  5. async: false,
  6. data: { &quot;managedObjects&quot;: [&quot;Audit&quot;,&quot;Logs&quot;]},
  7. contentType: &quot;application/json; charset=utf-8&quot;,
  8. dataType: &quot;json&quot;,
  9. complete: function (XMLHttpRequest, textStatus) {
  10. //File Handling
  11. }
  12. });

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"中传递一个列表

  1. $.ajax({
  2. ...
  3. url: "/export?managedObjects=Audit,Logs" ,
  4. ...
  5. });

在"Request Body"中传递一个列表

  1. $.ajax({
  2. type: "POST",
  3. url: "/export/",
  4. ...
  5. data: {managedObjects[0]: "Audit",
  6. managedObjects[1]: "Logs"}
  7. ...
  8. });
英文:

pass a list in Query Param

  1. $.ajax({
  2. ...
  3. url: &quot;/export?managedObjects=Audit,Logs&quot; ,
  4. ...
  5. });

pass a list in Request Body

  1. $.ajax({
  2. type: &quot;POST&quot;,
  3. url: &quot;/export/&quot;,
  4. ...
  5. data: {managedObjects[0]: &quot;Audit&quot;,
  6. managedObjects[1]: &quot;Logs&quot;}
  7. ...
  8. });

答案2

得分: 0

Here's the translated code portion:

尝试将您的数据转化为字符串:

  1. var data = {
  2. managedObjects: ["Audit", "Logs"]
  3. }
  4. $.ajax({
  5. type: "POST",
  6. url: "/export/",
  7. async: false,
  8. data: JSON.stringify(data),
  9. contentType: "application/json; charset=utf-8",
  10. dataType: "json",
  11. complete: function (XMLHttpRequest, textStatus) {
  12. }
  13. });

此外,您应该在@RequestParam中使用"name"而不是"value":

  1. @PostMapping(value = "/export")
  2. @ResponseBody
  3. public ResponseEntity<String> bulkExport(
  4. @RequestParam(name = "managedObjects", required = false) List<String> managedObjects) {
  5. //data
  6. }

请注意,我只翻译了您提供的代码部分,没有其他内容。

英文:

Try stringifying your data:

  1. var data = {
  2. managedObjects: [&quot;Audit&quot;, &quot;Logs&quot;]
  3. }
  4. $.ajax({
  5. type: &quot;POST&quot;,
  6. url: &quot;/export/&quot;,
  7. async: false,
  8. data: JSON.Stringify(data),
  9. contentType: &quot;application/json; charset=utf-8&quot;,
  10. dataType: &quot;json&quot;,
  11. complete: function (XMLHttpRequest, textStatus) {
  12. }
  13. });

Additionally you should use "name" instead "value" in @RequestParam:

  1. @PostMapping(value = &quot;/export&quot;)
  2. @ResponseBody
  3. public ResponseEntity&lt;String&gt; bulkExport(
  4. @RequestParam(name = &quot;managedObjects&quot;, required = false) List&lt;String&gt; managedObjects) {
  5. //data
  6. }

);

答案3

得分: 0

I think the problem is just with the list that you want to send in your request.

  1. var dataToSend = {
  2. list: [{ fieldname: 'ABC' }, { fieldname: 'DEF' }]; // your list should be something like this.
  3. $.ajax({
  4. type: "POST",
  5. //url: "policy/js_policy",
  6. url: "/export/?managedObjects=" + Mos,
  7. async: false,
  8. data: JSON.stringify(dataToSend),
  9. contentType: "application/json; charset=utf-8",
  10. dataType: "json",
  11. complete: function (XMLHttpRequest, textStatus) {
  12. }
  13. });

(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.

  1. var dataToSend = {
  2. list: [{ fieldname: &#39;ABC&#39; }, { fieldname: &#39;DEF&#39; }]; // your list should something like this.
  3. $.ajax({
  4. type: &quot;POST&quot;,
  5. //url: &quot;policy/js_policy&quot;,
  6. url: &quot;/export/?managedObjects=&quot; + Mos ,
  7. async: false,
  8. data: JSON.stringify(dataToSend),
  9. contentType: &quot;application/json; charset=utf-8&quot;,
  10. dataType: &quot;json&quot;,
  11. complete: function (XMLHttpRequest, textStatus) {
  12. }
  13. });

huangapple
  • 本文由 发表于 2020年7月31日 16:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定