2016-04-21 165 views
0

代码:我试图将一个阵列的数据发送到SpingMVC我无法从Angularjs'post请求得到DATAS在js文件

todelSome: function (targetArr) { 

     var args = {"arr": targetArr}; 
     var url = "toDelSome.req"; 

     return $http({ 
      method: 'POST', 
      data: args, 
      url: url 
     }).success(function (response, status, headers, config) { 


      return response; 
      console.log(response); 


     }).error(function (response, status, headers, config) { 

      alert(status + response); 

     }); 

    }" 

这里是我的代码用SpringMVC:

@ResponseBody 
@RequestMapping("toDelSome") 
public String toDelSome(@RequestParam("arr[]") Integer[] arr) { 

    System.out.println(arr); 

    return ""; 
} 

这里在控制台日志: enter image description here enter image description here

我怎样才能从Angularjs'request数组数据?

+0

不,这不是问题。我将其删除,问题仍然存在。 – JSO

+0

是你想要'toDelSome'方法返回数组吗? –

回答

0

试试这个

@ResponseBody 
@RequestMapping("toDelSome") 
public String toDelSome(@RequestParam("arr") Integer[] arr) { 

System.out.println(arr); 

return ""; 
} 
+0

相同。 400错误:必需整数[]参数'arr'不存在 – JSO

+0

您能显示targetArr吗? –

+0

你可以看到我发布的快照。我可以在Angular请求中获得targetArr,但我无法在SpirngMVC控制台中获得任何有关它的信息,甚至不会在“NULL” – JSO

0

去了很长的路要走创建一个类,带有一个数组,变量名应该是改编:

public class MockClass{ 
    Object[] arr; 
    public Object[] getArr() { return arr;} 
    public void setArr(Object[] arr) { this.arr = arr;} 
} 

data:{"arr": targetArr} 

然后换@RequestParam@RequestBody MockClass mock或尝试@ModelAttribute如果RequestBody没有按't work

+0

当我使用@RequestBody时,arr是NULL。当我们使用Ajax时,这种方法效果不错.Bang Angularjs似乎与jquery不同。这是让我困惑的地方。 – JSO

+0

我已编辑我的答案 –

+0

谢谢。我知道你的方法会运作良好。 @RequestBody必须映射一个java类。但临时数据不方便。如果我有其他数据,如String [],Long []等等,我必须创建他们的类。如果可能的话,我想找到一个像Jquery ajax的方法。 – JSO

0

Try this way :

js中的代码文件

todelSome = function(targetArr) { 
     var args = {arr : targetArr}; 
     var url = "toDelSome"; 

     $http.post(url, args).then(function(data, status, headers) { 

      return data; 
      console.log(data); 

     }, function(error) { 

      alert(error); 

     }); 
    }; 

守则控制器

@ResponseBody 
@RequestMapping("toDelSome") 
public String toDelSome(@RequestBody Integer[] arr) { 

    System.out.println(arr); 

    return ""; 
}