Angular 2:在离子2中使用邮枪发送电子邮件
我正在尝试使用Angular 2中的Mailgun向我的应用程序发送电子邮件通知。但是,当我执行Angular 2:在离子2中使用邮枪发送电子邮件,angular,ionic2,mailgun,Angular,Ionic2,Mailgun,我正在尝试使用Angular 2中的Mailgun向我的应用程序发送电子邮件通知。但是,当我执行send()函数时,出现了控制台错误。我不知道怎么解决,希望你能帮我提前谢谢。这是我的密码: import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { Http, Headers, RequestOptions } fr
send()
函数时,出现了控制台错误。我不知道怎么解决,希望你能帮我提前谢谢。这是我的密码:
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 = "sampleemail@gmail.com";
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 = "from=email@sample.com&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);
});
}
}
}
从'@angular/core'导入{Component};
从“离子角度”导入{IonicPage,NavController,NavParams};
从“@angular/Http”导入{Http,Headers,RequestOptions};
导入“rxjs/Rx”;
从“../tabs/tabs”导入{TabsPage};
@IonicPage()
@组成部分({
选择器:“页面确认器”,
templateUrl:'confirorder.html',
})
导出类确认页{
收件人:字符串=”sampleemail@gmail.com";
主题:string=“FirstTesting”;
message:string=“您好,这只是一个练习。”;
mailgunUrl:string=“”;
apiKey:string=“”;
tabBarElement:任何;
构造函数(公共http:http、公共navCtrl:NavController、公共navParams:navParams){
}
ionViewDidLoad(){
log('ionViewDidLoad ConfirmorderPage');
}
发送(){
if(this.recipient&&this.subject&&this.message){
让标题=新标题(
{
“内容类型”:“应用程序/x-www-form-urlencoded”,
“授权”:“基本”+此.apiKey
}
);
let options=newrequestoptions({headers:headers});
让身体离开=email@sample.com&to=“+this.recipient+”&subject=“+this.subject+”&text=“+this.message;
这是http.post(“https://api.mailgun.net/v3/“+this.mailgunUrl+”/messages”,正文,选项)
.map(result=>result.json())
.do(result=>console.log(“result:,JSON.stringify(result)))
.订阅(结果=>{
console.log(“已发送!”);
this.navCtrl.setRoot(TabsPage);
此项。收件人=”;
this.subject=“”;
this.message=“”;
},错误=>{
console.log(错误);
});
}
}
}
下面是控制台错误:
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.
XMLHttpRequest无法加载https://api.mailgun.net/v3//messages. 飞行前响应中的访问控制允许标头不允许请求标头字段授权。
CORS是chrome的一项功能,除非服务器允许,否则可防止跨域请求
在这种特殊情况下,服务器不接受授权标头
如果你是从你的后端做这件事,而不是chrome,这应该很好
从后端执行此操作,而不是从角度执行
此外,您不应该将api密钥/机密放在客户端上,因为它们会公开。谢谢,我需要如何修复此错误?我不明白。我是爱奥尼亚的新手,这是我第一次遇到这种问题。希望你能帮助我。不要在爱奥尼亚这样做,在你的服务器上这样做。在nodejs中你的意思是什么?如果你在使用nodejs,那么是的