私はReactで構築している静的サイトでformspreeを使用してフォームを送信しようとしています。私は近づいたが、今は完全に失われている。jQueryを使わずにReactでフォームを送信するAJAX
私はES6 Promise機能を使用しようとしていますが、完了方法はわかりません。すべてのヘルプをいただければ幸いです
Object {name: undefined, email: undefined, message: undefined}
:
import React from 'react';
import { Link } from 'react-router';
import { prefixLink } from 'gatsby-helpers';
import { config } from 'config';
import Headroom from 'react-headroom';
import Nav from './nav.js';
import '../css/main.scss';
import Modal from 'boron/DropModal';
import {Input, Label,Textarea, Button} from 're-bulma';
const modalStyle = {
minHeight: '500px',
backgroundColor: '#303841'
};
const backdropStyle = {
backgroundColor: '#F6C90E'
};
const contentStyle = {
backgroundColor: '#303841',
padding: '3rem'
};
const gotcha = {
display: 'none'
};
const email = 'https://formspree.io/[email protected]';
export default class RootTemplate extends React.Component {
static propTypes = {
location: React.PropTypes.object.isRequired,
children: React.PropTypes.object.isRequired,
}
static contextTypes = {
router: React.PropTypes.object.isRequired,
}
constructor() {
super();
this.showModal = this.showModal.bind(this);
}
showModal() {
this.refs.modal.show();
}
formSubmit(e) {
e.preventDefault();
let data = {
name: this.refs.name.value,
email: this.refs.email.value,
message: this.refs.message.value
}
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open('POST', email);
});
console.log(data);
}
render() {
return (
<div>
<Headroom>
<Nav showModal={this.showModal}/>
</Headroom>
<Modal ref="modal" modalStyle={modalStyle} contentStyle={contentStyle} backdropStyle={backdropStyle}>
<form ref='contact_form' onSubmit={::this.formSubmit}>
<Label>Name:</Label>
<Input ref="name" />
<Label>Email:</Label>
<Input ref="email" type="email"/>
<Label>Message:</Label>
<Textarea ref="message" />
<Input type="text" name="_gotcha" style={gotcha}/>
<Button buttonStyle="isOutlined" color="isWarning">Submit</Button>
</form>
</Modal>
{this.props.children}
</div>
);
}
}
私も現在、このエラーが出る:
は、ここに私の現在のコードです。本当に学ぶことを試みる。
(https://developer.mozilla.org/en-US/docs/Web/ JavaScript/Reference/Global_Objects/Promise)を見て、[非同期呼び出しからの応答を返すにはどうすればいいですか?](http://stackoverflow.com/q/14220321/218196) –
@FelixKling私の頭を包み込むのに苦労します。このプロジェクトの理由は、実際の例を使用することです。また、なぜ私がrefから未定義になっているのかわからない。 – Dileet
@Dileetエラーは、APIからですか?投稿は実行されていますか?ありがとう。 – chemitaxis