问题描述
我想使用http.get
方法来获取一些数据作为响应,但是当我想放置>
时会放%。
我的代码是:
let search = "class>"+`${this.class};&city=${this.city}`;
我的服务方法是:
getExtByClassAndCity( page_size: number,page: number,search: string,direction: string,order_by: string) {
const params = {
order_by,direction,page_size,search,page,}
return this.http.get<any>(`${env.apiURL}sips?`+`order_by=`+params.order_by+`&direction=`+params.direction+`&page_size=`+params.page_size
+`&search=`+params.search+`&page=`+params.page_size);
}
,发送网址是
sips?order_by=ext_sips.extension&direction=desc&page_size=5&search=class%3Exxxx;&city=&page=5
我的意思是课堂参数中的这个百分比。
请告诉我如何打印class>xxxx
而不是class%3Exxxx
。
解决方法
您可以创建一个HttpParams对象,并向其中添加参数:
const params = new HttpParams().set('order_by',order_by).set('direction',direction); // also for the other parameters
然后不要自己构造整个URL,而是将HttpParams添加到 get 调用
return this.http.get<any>(`${env.apiURL}sips`,{params});
,
请确保此%
不会对服务器的响应进行任何更改。
如果您看到我在请求的末尾放了page_size而不是page。现在它应该返回一个数组6的对象,但它只返回1行,我也不知道为什么!
如果您知道为什么,请告诉我。
您不能在URI中使用文字“>”字符,因此它会进行百分比编码。参见https://www.greenbytes.de/tech/webdav/rfc3986.html#percent-encoding。
,我对第一个答案的问题的答案是,因为我将pageIndex加到1,然后我将此pageIndex作为页面发送到URL中,但是我的后端页面从0开始而不是1,所以当我有6行时,它向我显示仅第二页的最后一行(因为page_size等于5)。 这是因为我通过服务器分页获取数据。