2017-08-27 107 views
0

我想发送页面编号和pagesize到webapi, 但没有参数传递,我debugedd应用程序在VS代码,pagingModel有pageSize和PageNumber成员,他们都有值,但是当我打开chrome dev工具时,请求中没有参数url 我的代码有什么问题吗?不能发送参数与角http.get

getTasks(pagingModel: IPagingModel): Promise<TaskList> { 
    const url = this.taskUrl; 
    const params = new URLSearchParams(); 
    let option: RequestOptions; 
    // tslint:disable-next-line:forin 
    for (const key in pagingModel) { 
     params.set(key, pagingModel[key]); 
    } 

    option = new RequestOptions({search: params, params: params}); 

    return this.http.get(url, option) 
       .toPromise() 
       .then(response => response.json() as TaskList) 
       .catch(this.handleError); 
    } 

enter image description here

+0

'搜索:params,params:params' - 调用参数名称及其变量'params'是一条冒险的道路。 –

+0

只是试图通过它所有可用的选项中得到, – Arash

回答

0

我的代码是好的,我刚刚导入URLSearchParams

import { Headers, Http, RequestOptions, RequestOptionsArgs, URLSearchParams } from '@angular/http'; 

我之前没有将其导入,我不知道为什么,但v代码给了我没有错误。

1

我不认为这是最好的方式。但也是这样,工作。

const url = this.taskUrl; 
url += '?'; 

Object.keys(pagingModel).forEach(key => { 
     url += `${encodeURIComponent(key)}=${encodeURIComponent(pagingModel[key])}&`; 
    }); 

return this.http.get(url) 
      .toPromise() 
      .then(response => response.json() as TaskList) 
      .catch(this.handleError); 

}

+0

这个作品,但我不知道这是否是最好的方式。 – Arash

+0

@Arash你的代码正在为我工​​作。我只是没有传递'params'键,只传递了'search'键。似乎'params'键不是一个有效的参数.'new RequestOptions({search:params});'。也无法理解为什么它不为你工作 – Rajez

0

你可以尝试一些像这样的事情在角V4

import {HttpParams} from "@angular/common/http"; 
const params = new HttpParams() 
    .set('pagenumber', '<your para>') 
    .set('pagesize', "<your para>"); 

this.http 
    .get("<requrl>", {params}) // mind this you dont have to use res.json() as httpClientModule has .json() by default 

我们通过链接集合构建的HttpParams对象() methods.because使用新HttpClientModule HTTPParams是不可变的,它的API方法 不会导致对象突变。相反,调用set将返回一个新的包含新值属性的HttpParams对象 。

下不会工作

params.set('pagenumber', '<para>') 
params.set('pagesize', "<para>"); 
+0

@ angular/common/http“;没有导出成员HttpParams – Arash

+0

@Arash你必须使用http客户端模块而不是http模块,将导入添加到'HttpClientModule'在进口阵列的'Ngmodule' –

+0

相同的错误,\t @角/普通/ HTT没有出口memebr HttpClientModule – Arash