0

Convert an array object to a json string using JSON.stringify

var array = [1, 2];
let json = JSON.stringify(array);
console.log(json);
axios.get('http://localhost/goods', json).then(function (res) {
    if (res.code == 200) {
        console.log("ok");
    }
}

Parameters during transmission with Chrome browser console:

enter image description here

My goods controller class, for example:

@RequestMapping(value = "goods",method = RequestMethod.GET)
public String deleteByIds(@RequestBody Integer[] ids) {
    goodsService.deleteByIds(ids);
    return "ok";
}

Spring mvc can't receive an array.Or am I having a problem with writing axios code? How to solve it?

1 Answer 1

1

From your request,

axios.get('http://localhost/goods', json)

It's a get request. So it won't have a body.

You can try changing the get method to post or use @RequestParameter instead of @RequestBody.

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

7 Comments

I just used @RequestParam, Cannot receive array. Because axios send an array of [1, 2]
When axios sends a get request to return:Uncaught (in promise) Error: Request failed with status code 400
Please help update your change in the question. If you use RequestParameter, you can test with simple url: /test?ids[]=1&ids[]=2 or use post method with body.
curl -X GET --header 'Accept: application/json' 'http://localhost:9090/goods?ids=1&2' This is no problem, your answer is good, but I am sure there is a problem with the axios code.
localhost:9090/goods?ids[]=1&ids[]=2 , how is the status and result?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.