Java – retrieves an array containing name value pairs from an Ajax request in spring MVC

I cannot retrieve the value in spring controller Who can explain what I did wrong?

Ajax request

fields[fieldID] = { 'name': fieldName,'value': fieldValue };
fieldID++;
$.ajax({ url: '/lic/register.html',data: { 'send': 'login-form','values': fields},type: 'get',complete : function(){
             alert(this.url)
         },success: function( output ) {
             alert("success");
         },});

Spring controller

@RequestMapping(value="/register.html",method = RequestMethod.GET)
@ResponseBody
public String suckRegister(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="values",required=false) String[] objectValues) {
    System.out.println(objectValues.length); // returning null
}

Solution

There seems to be no need for 'send': 'login form', unless another request parameter accepts the value in your controller

Try,

$.ajax({ url: '/lic/register.html',data: { 'values': fields},dataType: "json",contentType: "application/json",});

Your controller should accept

@RequestMapping(value="/register.html",@RequestParam(value="values[]",required=false) Object[] objectValues) {
    System.out.println(objectValues.length); // returning null
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>