JQuery Ajax how to pass an array type parameter

Suppose the parameters that need to be passed to the background are:

var userid = [1,2,3];

var username=["a","b","c"];

Var school = "Hahahahahaha";

There are two simple methods:

1. Join Traditional to prevent depth serialization

JS code:


$.ajax({
    url:"*****",
    data:{
        "userid":userid,
        "username":username,
        "school":school
    },
    dataType:"json",
    type:"post",
    Traditional: true, / / ​​Prevent depth serialization
    cache:false,
    async:false,
    success:function(){
        ....
    }
});

Java code:

@requesMapping(value="/****", method=RequestMethod.POST)
@responseBody
public String testForAjax(
                @RequestParam(value="userid") Integer[] userid, 
                @RequestParam(value="username") String[] username,
                String school){
  
      .....
 
}

 

 

2. Specify the parameter type

 

JS code:

$.ajax({
    url:"*****",
    data:{
        "userid":userid,
        "username":username,
        "school":school
    },
    dataType:"json",
    type:"post",
    cache:false,
    async:false,
    success:function(){
        ....
    }
});

Java code:

@requesMapping(value="/****", method=RequestMethod.POST)
@responseBody
public String testForAjax(
                   @RequestParam(value="userid[]") Integer[] userid,
                   @RequestParam(value="username[]") String[] username,
                   String school){
 
    .....
 
}

 

Intelligent Recommendation

AJAX requests JSON array data HTML code how to pass data as a parameter

Transmission data is an array SpeciesList as a parameter transmission Data with single double quotes as a parameter $("#goodsType h3").append("<span οnclick=\"tabShow(th...

Role of traditional parameters of the jQuery ajax ajax pass array parameters

In general, there may be some people in a parameter has more value, may be passed as a character-delimited form, such as a multiple checkbox on the page: Then get back and then separate parameters, th...

JQuery-form form Submit a plurality of ways to pass the plugin + AJAX parameter

Easy submission (modal frame) form Don't have a label to get a tag for a label There are two submissions method oneCommonly used for events, automatically submit by default SecondFor form submission A...

How jquery writes ajax and its parameter description

Method 1:   Method 2:     [Java Interview Questions and Answers] Organize recommendations Basics and grammar set network programming Concurrent programming Web Safety Design Patterns fr...

How to pass the array to the server (ajax C #)

Js to pass the array to the server C # receiving The editor did it temporarily, for reference only!...

More Recommendation

How to use AJAX pass strings array

A front end Backstage...

Request parameter is an array, how to pass parameters?

introduction: at work, sometimes after post will meet the parameters of the request for the case of an array, this situation should be how to solve it? As shown below:   Idea 1: directly into the...

How to pass array parameter in get request in vue

Foreword: When interacting with the backend in vue, use axios to send the request, and use the get request to pass the array type parameter without configuration. The backend cannot receive data, and ...

How to pass a bit array when the parameter of a function

Arrays are passed as formal parameters When an array is passed as a formal parameter, it is passed as a pointer. //Although the forms are different, the above two functions are equivalent, and the onl...

How to pass a two-dimensional array as a function parameter

A friend asked on the forum today: declare the function as void function (int** pInt), the intention is to pass a two-dimensional array of parameters. So we define a two-dimensional array, such as int...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top