Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringMVC中从ajax请求中检索包含名称-值对的数组_Java_Ajax_Spring_Model View Controller - Fatal编程技术网

Java 在SpringMVC中从ajax请求中检索包含名称-值对的数组

Java 在SpringMVC中从ajax请求中检索包含名称-值对的数组,java,ajax,spring,model-view-controller,Java,Ajax,Spring,Model View Controller,我无法检索Spring控制器中的“值”。谁能解释我做错了什么 Ajax请求 fields[fieldID] = { 'name': fieldName, 'value': fieldValue }; fieldID++; $.ajax({ url: '/lic/register.html', data: { 'send': 'login-form', 'values': fields}, type: 'get', complete : fun

我无法检索Spring控制器中的“值”。谁能解释我做错了什么

Ajax请求

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");
         },

      });
弹簧控制器

@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
}

似乎不需要“发送”:“登录表单”,除非另一个请求参数在控制器中接受该值

@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
}
试试看

您的控制器应该接受

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

谢谢你的回复。我已经试过了,但我仍然在控制器中的“System.out.println(objectValues.size());”处得到了nullexception。@dimley如果您使用类似于postman的工具尝试请求,会发生什么?使用postman对url进行了验证,结果似乎没有问题。但是在服务器端得到同样的错误。这是我的url:“你能试试吗,数据:{'values':JSON.stringify(fields)}我试过了,但仍然得到相同的错误。是否有其他方法将名称-值对从ajax发送到控制器?