jquery ajax traditional:true

Generally, when a parameter has multiple values, some people may pass it in a character-separated form, such as multiple checkboxes on a page:
$.ajax{
      url:"xxxx",
      data:{
            p: "123,456,789"
      }
}
Then the parameters are obtained in the background and then separated. This approach has great drawbacks. What happens if the character used for the separator appears in a parameter value? Of course, the parameters obtained in the background will not match the actual situation.
At this time, I thought of writing the above code as follows:
$.ajax{
      url:"xxxx",
      data:{
            p: ["123", "456", "789"]
      }
}
If it is simply written like this, the parameters cannot be obtained in the java background, because jQuery needs to call jQuery.param to serialize the parameters,
jQuery.param( obj, traditional )
by default, traditional is false, that is, jquery will serialize the parameters deeply Objects to suit frameworks such as PHP and Ruby on Rails,
but servelt api can't handle it, we can prevent deep serialization by setting traditional to true, then the serialization result is as follows:
p: ["123", "456", "789" ] =>
Immediately, we can obtain the parameter value array through request.getParameterValues() in the background,
so, for example, if we have multiple checkboxes in the foreground, the code in the foreground can be written as:
var values ​​= $("input[type=checkbox]" ).map(function(){
      return $(this).val();
}).get();
$.ajax{
      url:"xxxx",
      traditional: true,
      data:{
            p: values 
      ​​}
}


Is the code Beautiful and much more robust?

Guess you like

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