我想发送JSON请求
for (let i = 0; i < this.myLaptopNamesList.length; i++) {
alert(""+i);
let LaptopMake1 = 'LaptopMake'+i;
let LaptopSerial1='LaptopSerial'+i;
alert(""+LaptopMake1+""+LaptopSerial1)
alert(""+this.myLaptopNamesList[i].name+""+this.myLaptopNamesList[i].number)
let customLaptop: {};
customLaptop = {
LaptopMake1: this.myLaptopNamesList[i].name,
LaptopSerial1: this.myLaptopNamesList[i].number
};
this.newCusomLaptop = customLaptop;
}
我从我的for循环获得了laptopMake和LaptopSerial。我该如何发送请求?
答案 0 :(得分:0)
您需要使用Http
@angular/http
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
@Injectable()
export class AppProvider {
constructor(private http: Http) {}
public postRequest(customLaptop: any):Observable<SearchCriteriaObject> {
let url:string = 'url you want to post to';
return this.http.post(url, customLaptop)
.map((res:Response) => res.json());
}
}
在你的component.ts中你可以调用这个方法并订阅响应。
import { AppProvider } from 'location of app provider';
export class Component {
constructor(private provider: AppProvider){}
this.provider.postRequest(this.customLaptop)
.subscribe((data: any) => {
// do something on success
}, (error: any) => {
// do something on error
})
}