【问题标题】:Angular HTTPClient POST body not sendingAngular HTTPClient POST 正文未发送
【发布时间】:2018-07-02 04:41:47
【问题描述】:

目前我正在尝试将名称、电子邮件和消息从 Angular 前端发布到在同一 nginx 服务器中运行的 php 脚本,然后运行 ​​phpmailer 以发送包含名称、电子邮件和消息的电子邮件。到目前为止的代码如下:

import { Component, OnInit } from '@angular/core';
import { Email } from './email';
import {ContactService} from './contact.service';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {NgForm} from '@angular/forms';

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/x-www-form-urlencoded'
  })
};

const email = new Email('', '', '');

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.css'],
  providers: [ContactService]
})
export class ContactComponent implements OnInit {

  email = new Email('', '', '');

  constructor(private http: HttpClient) { }

  sendEmail(form: NgForm) {
    const value = form.value;
    const senderName = value.name;
    const senderEmail = value.email;
    const senderMessage = value.message;
    this.sendMail(senderName, senderEmail, senderMessage);
  }

  sendMail(senderName, senderEmail, senderMessage) {
    console.log(senderName + ' ' + senderEmail + ' ' + senderMessage);
    this.http.post('https://ruffstuffcostumes.tk/assets/scripts/email.php',
      {
        name: senderName,
        email: senderEmail,
        message: senderMessage,
      },
      httpOptions
      )
      .subscribe(
        (val) => {
          console.log('POST call successful value returned in body',
            val);
        },
        response => {
          console.log('POST call in error', response);
        },
        () => {
          console.log('The POST observable is now completed.');
        });
  }

  ngOnInit() {
  }

}

当我通过邮递员运行 POST 请求来检查它时,它运行得非常好并发送了包含所需元素的电子邮件,但是当我使用此脚本执行查询时,即使 console.log(senderName + ' ' + senderEmail + ' ' + senderMessage) 确实显示了值,它似乎根本没有将它们发布在正文中,而我得到的只是这样一个事实,即使发送了一封邮件,邮件正文中也没有任何这些值。

可能是跨域问题(如果是的话,最好的解决方法是什么?),还是我只是犯了一些愚蠢的错误?

【问题讨论】:

  • 如果你在浏览器开发工具(F12)中检查,然后是网络选项卡,你能看到正在发送的内容吗?
  • @user184994 是的,它正在发送:https://url/assets/scripts/email.php?name=username&email=email@protonmail.com&message=hi,%20it%20works!! 所以它似乎确实在分发数据,但奇怪的是 php 脚本似乎不想接收它
  • 它还发送的标头是:POST /assets/scripts/email.php?name=username&email=email@protonmail.com&message=hi,%20it%20works!! HTTP/1.1 Host: url Connection: keep-alive Content-Length: 0 Accept: application/json, text/plain, */* Origin: https://url User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3477.0 Safari/537.36 DNT: 1 Referer: https://url/ Accept-Encoding: gzip, deflate, br Accept-Language: en-GB,en-US;q=0.9,en;q=0.8 Cookie: __cfduid=d41abcfedcaa744a8de652dcbe37dab021530345277
  • 是的,有什么理由不使用 JSON 吗?
  • @user184994 主要是因为我不知道如何在 php 中进行 json POST body 解码。还有,没想到

标签: javascript node.js angular post angular-httpclient


【解决方案1】:

只是为了完整性。我通过切换到发送 JSON 数据并在 php 端解析它解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2018-07-05
    • 2019-10-05
    • 2018-05-26
    • 1970-01-01
    • 2021-07-27
    • 2019-12-19
    • 1970-01-01
    相关资源
    最近更新 更多