显示临时标题 - Nodemailer - AJAX

时间:2018-03-14 10:36:21

标签: javascript ajax request nodemailer

请求标头始终向我发出警告:显示临时标头。 我无法弄清楚为什么我会遇到这个问题。由于这个警告,readyState没有改变,并且我在eventhandler onreadystatechange上的回调不会触发。我想它与application / json请求标题有关,但我还没有找到解决方案。 希望有人能够解决这个问题。 我的AJAX代码:

export default function ajax() {
  const form = document.forms[0];

  const xhr = new XMLHttpRequest();

  form.addEventListener('submit', (e) => {
    e.preventDefault();

    // FORMDATA TO JSON OBJECT
    const fields = document.querySelectorAll('form > div');
    let formData = {};
    fields.forEach((container) => {
      formData[container.childNodes[0].name] = container.childNodes[0].value;
    })
    const formDataJSON = JSON.stringify(formData);

    xhr.open('POST', '/send', true);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

    xhr.onreadystatechange = function() {
      if(this.readyState === this.DONE && this.status === 200)
        console.log('success');

      else if(this.readyState === this.UNSET)
        console.log('failed');

      else if(this.readyState === this.LOADING)
        console.log('loading');
    }

    xhr.send(formDataJSON);
  })
}

Nodemailer:

module.exports = (data) => {
  const _auth = require('./_auth');
  const nodemailer = require('nodemailer');

  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: _auth.user,
      pass: _auth.pass
    }
  });

  const html = `
    <div style="background-color: #F3F4F7; border-radius: 10px; padding: 50px;">
      <h2>Sie haben eine neue Nachricht</h2>
      <p>${data.nachricht}</p>
      <h3>Absender</h3>
      <ul>
        <li>Name: ${data.name}</li>
        <li>Email: ${data.email}</li>
        <li>Tel: ${data.tel}</li>
      </ul>
    </div>
  `;

  const mailOptions = {
    from: data.email,
    to: _auth.user,
    subject: '20° - Nachricht',
    text: data.nachricht,
    html
  };

  transporter.sendMail(mailOptions, function(error, info){
    if (error) 
      console.log(error);
    else 
      console.log('Email sent: ' + info.response);

    transporter.close();
  });
}

0 个答案:

没有答案