How to pass array of strings using ajax

 By using ajax to send a string array to the background, the background cannot receive the value?

$.ajax({
            type: "POST",
            url: url,
            data: {contentIds:contentIds,channelId:channelId},
            dataType: "json",
            success: function(data){
                if(data.msg){
                alertx(succ,function(){
                var f = getTableForm();
                                 f.action="####";
                                 f.submit();
                });
                }
            },
            error:function(){
            alertx("Not authorized to perform this operation!");
            }
        });

Background code:

@RequestMapping(value = "articleMove")
public @ResponseBody String articleMove(String[] contentIds,String channelId) {}

Seen through browser observation and background interception parameters

- contentIds[]:6c7dc887815041f5a787435eb6e3d7e5
- channelId[]:bb2cf2a2e9c54524a8971eeb987541b6


It is found that there will be [] in the parameter and cannot be bound to it. Add traditional: true in the attribute of ajax, // set to true here

The background can be accessed and the data can be successfully received.

$.ajax({
            type: "POST",
            url: url,
            data: {contentIds:contentIds,channelId:channelId},

            traditional: true,//这里设置为true
            dataType: "json",
            success: function(data){
                if(data.msg){
                 alertx(succ,function(){
                  var f = getTableForm();
                                 f.action="####";
                                 f.submit();
                 });
                }
            },
            error:function(){
             alertx("没有权限执行此操作!");
            }
        });

- contentIds:6c7dc887815041f5a787435eb6e3d7e5
- channelId:bb2cf2a2e9c54524a8971eeb987541b6


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394627&siteId=291194637