-2

I want to use array as a parameter of get method of Angular. I use codes like below.

import { Http } from '@angular/http'; 
constructor(private http: Http) {}
this.http.get('http://localhost:5000/test',{
 params: {user}
})

user is defined as {id: number=101, name: string='john'}

and I expect to generate URL

http://localhost:5000/test?id=101&name=john

However, generated URL is

http://localhost:5000/test?user=%7B%id%22:%22YV1AMbiy%22,%22name%22:%22345678%22

4

4 回答 4

1

您可以使用

let params = new HttpParams()
    .set('id', 101)
    .set('name','john');

 return this._HttpClient.get(URL, { params: params })
于 2018-10-14T12:56:10.293 回答
0

从 get 方法的参数定义中删除 {}

import { Http } from '@angular/http'; 
constructor(private http: Http) {}
this.http.get('http://localhost:5000/test', params: user )
于 2018-10-14T12:51:04.003 回答
0

您可以JSON.stringify在 Angular中使用

this.http.get(
    `http://localhost:5000/test`,
    {
        params: { user: JSON.stringify(user) },
    },
);

在您的后端,您可以使用:

const user = JSON.parse(req.query.user)
于 2018-10-14T12:57:28.443 回答
0

而不是 params: {user}应该这样做params: user。否则,它将被转译为params: { 'user': user }

于 2018-10-14T12:52:34.057 回答