2
我試圖在Angular 2中使用Mailgun將電子郵件通知發送到我的應用程序。但是當我執行send()
函數時,我收到控制檯錯誤。我不知道如何解決它,希望你能提前幫助我。這裏是我的代碼:Angular 2:在Ionic 2中使用Mailgun發送電子郵件
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http, Headers, RequestOptions } from "@angular/http";
import "rxjs/Rx";
import { TabsPage } from '../tabs/tabs';
@IonicPage()
@Component({
selector: 'page-confirmorder',
templateUrl: 'confirmorder.html',
})
export class ConfirmorderPage {
recipient: string = "[email protected]";
subject: string = "FirstTesting";
message: string = "Hello, this is just a drill.";
mailgunUrl: string = "<--My Domain-->";
apiKey: string = "<--My API Key-->";
tabBarElement: any;
constructor(public http: Http, public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad ConfirmorderPage');
}
send() {
if(this.recipient && this.subject && this.message) {
let headers = new Headers(
{
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + this.apiKey
}
);
let options = new RequestOptions({ headers: headers });
let body = "[email protected]&to=" + this.recipient + "&subject=" + this.subject + "&text=" + this.message;
this.http.post("https://api.mailgun.net/v3/" + this.mailgunUrl + "/messages", body, options)
.map(result => result.json())
.do(result => console.log("RESULT: ", JSON.stringify(result)))
.subscribe(result => {
console.log("SENT!");
this.navCtrl.setRoot(TabsPage);
this.recipient = "";
this.subject = "";
this.message = "";
}, error => {
console.log(error);
});
}
}
}
,這裏是控制檯錯誤:
XMLHttpRequest cannot load https://api.mailgun.net/v3/<--My Domain-->/messages. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
謝謝,我該如何解決這個錯誤?我不明白。我是Ionic的新手,也是我第一次遇到這種類型的問題。希望您能夠幫助我。 – Patrick
不要在離子狀態下執行此操作,請在您的服務器上執行此操作。 – misha130
in nodejs你的意思是? – Patrick