【问题标题】:Send HTTP Request to NodeJS from Angular 4从 Angular 4 向 NodeJS 发送 HTTP 请求
【发布时间】:2018-02-20 02:31:53
【问题描述】:

我想弄清楚如何从 Angular 4 发送电子邮件。 我已经配置了 NodeJS,并且还使用 mailgun 创建了帐户。

我的请求文件

var headers = new Headers(); 
headers.append("Authorization", "Basic "+btoa("api:key-XXXXXXXXXXXXX"));
var url="https://api.mailgun.net/v3/DOMAIN.mailgun.org.mailgun.org/messages";
var mail = {
  from : "text",
  to : "text",
  subject : "text",
  text : "text"
};
this._http.post(url, message, {headers:authHeader});

我不明白如何将 Angular 与 Node js 连接起来

【问题讨论】:

    标签: node.js angular email


    【解决方案1】:

    提供并接受的答案是针对 AngularJS (1.x)。 Angular 2+ 中根本没有 $scopemodule()controller()

    documentation 向 POST 添加标头:

    let message = {
      from : "text",
      to : "text",
      subject : "text",
      text : "text"
    };
    
    this.http.post('/some/api/path', JSON.stringify(message), {
        headers: new HttpHeaders().set('Authorization', "Basic " + btoa("api:key-XXXXXXXXXXXXX"))
    })
    .subscribe(res => console.log(res));
    

    或者使用HttpHeaders 对象:

    let foo: string = btoa("api:key-XXXXXXXXXXXXX");
    let headers = new HttpHeaders({ 'Authorization': "Basic " + foo )});
    
    this.http.post('/some/api/path', JSON.stringify(message), { headers: headers })
        .subscribe(res => console.log(res));
    

    前面的示例是针对 Angular 4+ HttpClientModule,如果您使用的是 Angular 2.x 和 HttpModule,那么您可以像 this 一样使用 HeadersRequestOptions 来处理它:

    import { Headers, RequestOptions } from '@angular/http';
    
    let message = { /* ... */ };
    
    let foo: string = btoa("api:key-XXXXXXXXXXXXX");
    let headers = new Headers({ 'Authorization': "Basic " + foo )});
    let options = new RequestOptions({ headers: headers });
    
    this.http.post('/some/api/path', JSON.stringify(message), options)
        .subscribe(res => console.log(res));
    

    希望对您有所帮助!

    【讨论】:

    • 现在我收到此错误:“'Headers' 类型的参数不可分配给“RequestOptions”类型的参数' export class Enrollment Component { constructor(protected http: Http) { let foo: string = btoa("api:key-XXXXXXXXXXXXX"); let headers = new HttpHeaders({ 'Authorization': "Basic " + foo }); let options = new RequestOptions({ headers: headers }); this.http.post( '/some/api/path', JSON.stringify(message), options) .subscribe(res => console.log(res));
    • 听起来您可能正在使用 HttpModule,而不是 Angular 4+ HttpClientModule。您需要将RequestOptionsHeaders 一起使用。我已经更新了答案来证明这一点。希望有帮助!
    • 我收到一个错误:'from' 参数丢失。 message mwssage= { from : 'email@gmail.com', to : 'email@gmail.com', subject : 'text', text : 'text', multipart : true}; foo: string = btoa("api:key-xxxxxxxx"); headers = new HttpHeaders({ 'Authorization': "Basic" + this.foo }); send(){ this.http.post('api.mailgun.net/v3/sandboxc.mailgun.org/messages', JSON.stringify(this.message), { headers: this.headers }) .subscribe(res => console.log(res));
    • 您可能需要发布一个单独的问题,因为这是关于 HTTP 标头的。目前尚不清楚您何时看到此错误。我的猜测是this.message 是未定义/空的。您需要确保在创建对象时正在执行this.message = { /* props */ }。尝试在 POST 之前执行 console.log(this.message) 并查看您是否有价值观,这将是一个很好的起点。
    猜你喜欢
    • 2016-11-15
    • 2018-05-04
    • 2013-01-03
    • 2016-12-27
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多