未处理的拒绝(SyntaxError):意外的输入结束

时间:2018-05-23 16:05:06

标签: javascript reactjs mailchimp

我正在制作一个专门用来接收电子邮件订阅者的组件......就像这样

import React from "react";

class SubscribeOffer extends React.Component {
constructor () {
    super();
    this.state={
        email: ""
    }
}

fillEmailToSubscribe = (event) => {
    this.setState({email: event.target.value});
}

submitEmailToSubscribe = () => {
    if (this.state.email > 3) {
        fetch('https://(myuser).list-manage.com/subscribe/post?u=(somenumber)&id=(myid)'
        ,{
            mode: 'no-cors',
            method:'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                email:this.state.email
            })
        })
        .then(response => response.json())
        .then(() => {
            alert('Thank you for subscribing!');
        })
    }

}

render () {
    return (
        <div>
            <h1>Get updated upcoming events in your inbox everyday!</h1>
            <p>Would you like us to give you updates with future updates upcoming events?</p>
            <input 
                type='email'
                name='email-address'
                id='email-address'
                placeholder="your email" 
                onChange={this.fillEmailToSubscribe}
                required
            />
            <button onClick={this.submitEmailToSubscribe}>Subscribe</button>
        </div>
    )
}
}

export default SubscribeOffer;

但是,我只得到像这样的回复

Unhandled Rejection (SyntaxError): Unexpected end of input

当我检查API如何响应时,它会给我这样的预览 enter image description here

你知道我怎么能解决这个问题? 我这样做就像人们输入他们的电子邮件一样简单,只需点击按钮即可。

非常感谢您的任何帮助!

1 个答案:

答案 0 :(得分:0)

由于您的电子邮件是在构造函数中定义的字符串,并且您正在将该电子邮件与该数字进行比较,这将是您的第一个错误。

因此,您可以从&#39; submitEmailToSubscribe&#39;更新if循环。功能类似于:

if(this.state.email.trim() !== '') {
  fetch('https://(myuser).list-manage.com/subscribe/post?u=(somenumber)&amp;id=(myid)'
    ,{
        mode: 'no-cors',
        method:'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            email:this.state.email
        })
    })
    .then(() => alert('Thank you for subscribing!'));
}