0

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);
    }
3
  • 1
    If you want to send it with a get request, you have to manually split them up or encoded them in some way. The easiest solution though is to wrap the array in a JSON object and post that object to the server. Commented Feb 5, 2018 at 23:45
  • are you using spring boot? Commented Feb 5, 2018 at 23:47
  • Yes, it is spring boot. Commented Feb 5, 2018 at 23:56

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Based on Ranielle's answer I proceeded with HttpPost.

Here is the code:

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

And the Spring side

@RequestMapping(value = "server", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<MyObjects> sort( @RequestBody List<MyObjects> query) {
        List<MyObjects> results2 = new ArrayList<>();

        for(MyObjects a : query) {
                System.out.println(a.getFirstName());
        }

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.