使用Angular的$ http通过GET请求将对象数组发送到Java Spring [英] Send array of objects via GET request with Angular's $http to Java Spring

查看:261
本文介绍了使用Angular的$ http通过GET请求将对象数组发送到Java Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript变量,它是MyObjects的数组.我可以使用以下代码在视图中显示此变量:

I have a javascript variable which is an array of MyObjects. I can display this variable to the view with the following code:

        <tr ng-repeat="user in lala.users">
            <td>{{ user.firstName }}</td>
            <td>{{ user.lastName }}</td>
        </tr>

现在,我正在尝试将此数组发送到服务器.我正在尝试类似的东西:

Now I am trying to send this array to the server. I am trying something like:

    lala.send = function() {
        $http({
            method: 'GET',
            url: "http://localhost:8080/server/" + lala.users
        }).then(function successCallback(response) {
            if (response.status == 200) {
                lala.users = response.data
            }
        });
    };

如何将该数组传递到服务器端的列表?直到现在我有这样的东西.

How can I pass this array to a list on the server side? Till now I have something like this.

@RequestMapping(value = "/server/{users}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<MyObjects>> transform(@PathVariable("users") String[] users) {
        List<MyObjects> results2 = new ArrayList<>();

        //pass array to a list??

        return new ResponseEntity<>(results2, HttpStatus.OK);
    }

推荐答案

您可以转换字符串并使用定界符,然后在服务器端对其进行解析.您还可以使用 HttpPost 代替 HttpGet 请求,以便发送JSON字符串,并且更易于解析.

You can transform your string and use a delimiter, then parse it on your server side. You can also use HttpPost instead of HttpGet request so you can send JSON String and it is easier to parse.

这篇关于使用Angular的$ http通过GET请求将对象数组发送到Java Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆