Javascript 将请求转换为Angular 2并键入脚本
有一个Post请求Javascript 将请求转换为Angular 2并键入脚本,javascript,angular,typescript,Javascript,Angular,Typescript,有一个Post请求 var settings = { "async": true, "crossDomain": true, "url": "http://localhost:32001/api/job/jobs-list-excel", "method": "POST", "headers": { "content-type": "application/json", "cache-control": "no-cache", "postman-tok
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:32001/api/job/jobs-list-excel",
"method": "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": "9135e481-df69-b870-4f72-92873c1fd7de"
},
"processData": false,
"data": "{\"jobIds\": [2,3]}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我在Angular 2中做,但她不工作,为什么?
在请求中,我需要传输项目ID列表。
作为回报,获取xls文件,但我不能这样做。我提出了一个post请求,但我无法将其转换为一个角度
public GetJobsListExcel(jobs: JobViewModel[]): Promise<Result> {
var JobIds: number[] = [];
var headers = new Headers();
headers.append('Content-Type', 'application/json')
jobs.forEach(x => JobIds.push(x.JobId));
return this.http.post(this.jobsUrl + "/jobs-list-excel",
{
jobIds: JobIds,
},
{
headers: headers
})
.toPromise()
.then(response => response.json() as Result);
}
公共GetJobsListExcel(作业:JobViewModel[]):承诺{
var JobIds:number[]=[];
var headers=新的headers();
headers.append('Content-Type','application/json')
jobs.forEach(x=>JobIds.push(x.JobId));
返回this.http.post(this.jobsUrl+“/jobslist excel”,
{
jobIds:jobIds,
},
{
标题:标题
})
.toPromise()
.then(response=>response.json()作为结果);
}
以JSON.stringify({jobIds:jobIds})的形式发送数据与您在上面的post请求中发送数据的方式相同。那么最后一个请求的内部响应是什么?你能
console.log(response)
并报告结果吗。还将拒绝处理程序和console.log()放在那里。更改.then(response=>response.json()作为结果)然后(response=>console.log(response),err=>console.log(err))代码>然后您可以返回报告。我看不到您要在标题->的“邮差令牌”中设置令牌:“9135e481-df69-b870-4f72-92873c1fd7de”
import {Headers, RequestOptions} from 'angular2/http';
let body = JSON.stringify({ 'jobIds': '[2,3]' });
let headers = new Headers({ 'Content-Type': 'application/json' },{'cache-control':'no-cache'},{'postman-token':'9135e481-df69-b870-4f72-92873c1fd7de'});
let options = new RequestOptions({ headers: headers });
let url = 'http://localhost:32001/api/job/jobs-list-excel';
return this.http.post(url, body, options)
.map(res => res.json().data)
.catch(this.handleError)