如何在Angular 2中提出这样的请求?

时间:2017-05-30 08:06:56

标签: angular typescript

有这样的要求:

public GetJobsListExcel(jobs: JobViewModel[] ): Promise<string> {
        let JobIds: number[] = [];
        jobs.forEach(x => JobIds.push(x.JobId));
        let url = `/api/job/jobs-list-excel?jobIds=${JobIds}`;
        return this.http.get(url)
            .toPromise()
            .then(response =>
                response.text() as string
            );
    }

它会生成如下参考:/ api / job / jobs-list-excel? JobIds = $ 1,2,3 e.t.c 你需要的是这样的:/ api / job / jobs-list-excel? JobIds = 1美元? JobIds = 2美元? JobIds = $ 3

我试过大家,不出门,朝什么方向挖?谢谢。

2 个答案:

答案 0 :(得分:0)

我建议您将JobIds传递给API ,分隔的number列表作为字符串

let url = `/api/job/jobs-list-excel?jobIds=${JobIds.join(',')}`

服务器端split JobIds字符串,(逗号)

答案 1 :(得分:0)

如果您需要在问题中提出多个JobIds查询字符串

,这可能会有效
let url = `/api/job/jobs-list-excel?JobIds=${JobIds.join('&JobIds=')}` 
// => "/api/job/jobs-list-excel?JobIds=1&JobIds=2&JobIds=3&JobIds=4"