2016-12-12 4 views
0

私はangular2で作業しています。私はサーバーにPOST要求を送信したいと思います。ionic2でサーバーにPOST要求を行う方法

リクエストの送信方法を教えてください。使用される実際のコードがすることができ、事前

+0

でAPI

import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class ApiService { constructor(private http: Http) { console.log('ApiService init'); } postSample() { let param = { val1:'test'}; return this.http.post('http://sample.com', param ).map(res => res.json()); } } 

使用することを:http://stackoverflow.com/questions/ 41033895/http-post-to-fcm-server-not-working/41034358?noredirect = 1#comment69405377_41034358私はちょうどfcmサーバのために働いていました – dhruv

答えて

1

ありがとう:

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

...... 

constructor(public http: Http) { } 

sendPushNotification(deviceId: string) { 
    let url = 'https://fcm.googleapis.com/fcm/send'; 
    let body = 
    { 
    "notification": { 
     "title": "Notification title", 
     "body": "Notification body", 
     "sound": "default", 
     "click_action": "FCM_PLUGIN_ACTIVITY", 
     "icon": "fcm_push_icon" 
    }, 
    "data": { 
     "hello": "This is a Firebase Cloud Messagin hbhj g Device Gr new v Message!", 
    }, 
    "to": "device token" 
    }; 
let headers: Headers = new Headers({ 
    'Content-Type': 'application/json', 
    'Authorization': 'key='+this.someKey 
}); 
let options = new RequestOptions({ headers: headers }); 

console.log(JSON.stringify(headers)); 

    this.http.post(url, body, headers).map(response => { 
    return response; 
    }).subscribe(data => { 
    //post doesn't fire if it doesn't get subscribed to 
    console.log(data); 
    }); 
} 
+0

私は以下の行、コンストラクタ(public http:Http) [ts]はHTTPを見つけることができません.. – laxman

+0

'@ angular/http'からのインポート{Http、Headers、RequestOptions}を使用しました。 ? – dhruv

+0

this.http.post(url、JSON.stringify(dataBody)、headers).subscribe(data => { console.log(data); });上記のコードは、私は投稿要求を送信するために使用していますが、データはURLに入れられています...要求の本文に入るべきです。 – laxman

0

ポストのDeclare ApiServiceあなたがチェックアウトすることができ活字体

this.ApiService.postSample().subscribe(data => { 
     console.log(data); 
    }); 
関連する問題