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

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

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 = &quot;/export&quot;)
    @ResponseBody   
	public ResponseEntity&lt;String&gt; bulkExport(
		@RequestParam(value = &quot;managedObjects&quot;, required = false) List&lt;String&gt; managedObjects) {
        //data
        }
   );

I want to send AJAX POST request.

$.ajax({
      type: &quot;POST&quot;,
      //url: &quot;policy/js_policy&quot;,
      url: &quot;/export/ ,
      async: false,
      data: { &quot;managedObjects&quot;: [&quot;Audit&quot;,&quot;Logs&quot;]},
      contentType: &quot;application/json; charset=utf-8&quot;,
      dataType: &quot;json&quot;,
      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: &quot;/export?managedObjects=Audit,Logs&quot; ,
      ...
    });

pass a list in Request Body

$.ajax({
  type: &quot;POST&quot;,
  url: &quot;/export/&quot;,
  ...
  data: {managedObjects[0]: &quot;Audit&quot;,
         managedObjects[1]: &quot;Logs&quot;}
  ...
});

答案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: [&quot;Audit&quot;, &quot;Logs&quot;]
}
$.ajax({
  type: &quot;POST&quot;,
  url: &quot;/export/&quot;,
  async: false,
  data: JSON.Stringify(data),
  contentType: &quot;application/json; charset=utf-8&quot;,
  dataType: &quot;json&quot;,
  complete: function (XMLHttpRequest, textStatus) {
    
  }
});

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

@PostMapping(value = &quot;/export&quot;)
@ResponseBody   
public ResponseEntity&lt;String&gt; bulkExport(
    @RequestParam(name = &quot;managedObjects&quot;, required = false) List&lt;String&gt; 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: &#39;ABC&#39; }, { fieldname: &#39;DEF&#39; }]; // your list should something like this.
    $.ajax({
      type: &quot;POST&quot;,
      //url: &quot;policy/js_policy&quot;,
      url: &quot;/export/?managedObjects=&quot; + Mos ,
      async: false,
      data: JSON.stringify(dataToSend),
      contentType: &quot;application/json; charset=utf-8&quot;,
      dataType: &quot;json&quot;,
      complete: function (XMLHttpRequest, textStatus) {
        
      }
    });

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:

确定