2017-09-08 4 views
-1

に私のサーバーにJSONリクエストを送信し、私は私のforループからlaptopMakeとLaptopSerialを得たJSON要求は、イオン2

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; 
} 

を送りたいです。どのようにリクエストを送信できますか?

+0

これまでに何を試みましたか?使用したコードを共有できますか? –

答えて

0

あなたは、このメソッドを呼び出し、応答を購読するでしょう、あなたのcomponent.ts内部@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()); 
    } 
} 

からHttpを使用する必要があります。

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 
     }) 
}