Home > front end >  Axios pass an array as problems get request? Hk []=[1, 2, 3]
Axios pass an array as problems get request? Hk []=[1, 2, 3]

Time:11-04

Axios use get the params transfer directly into an array of

I write so
 
Var nk=[1, 2, 3]
Axios. Get (urlStr, {
Params: {
Hk: nk
}
})

Single request url is joining together for localhost: 8080/API/? Hk []=[1, 2, 3]
Turned into a 400 illegal format how to splice it into the right localhost: 8080/API/? Hk=1 & amp; Hk=2 & amp; Nk=3


Thank you all for this problem has been bothering me for days

CodePudding user response:

You look it

CodePudding user response:

This is handle by yourself, don't put params went inside, his hand in the url to deal with the
 
Var nk=[1, 2, 3]
UrlStr +=?
Nk. ForEach (e=& gt; {
UrlStr +="hk=" + e + "& amp;"
})
UrlStr=urlStr. Replace (/& amp; $/, "")
Axios. Get (urlStr)

CodePudding user response:

reference 1st floor qq_35984791 response:
help everybody see

Qs plug-in library should be able to solve the

CodePudding user response:

 
Qs. Stringify ({a: [' b ', 'c']}, {arrayFormat: 'repeat'})

CodePudding user response:

Let the background change interface, also can do this by value? Make a hammer?

CodePudding user response:

Generally simple digital string with get a complex type with a post

CodePudding user response:

Upstairs + 1, qs library perfect solution, it is very convenient,
In addition, why don't want to get? A heap of url doesn't look good, do not post directly to send json his sweet?
Finally, provide a way:
 
Let arr=[1, 2, 3];
Let the result='? '+ arr. The map ((item, index)=& gt; ` item ${index}=${item} `). Join (' & amp; ');
console.log(result);//output is'? Item1=1 & amp; Item2=2 & amp; Item3=3 '