【发布时间】:2019-06-04 18:16:51
【问题描述】:
我正在尝试进行此调用,其中 url 是基本域名,model 是传递到 POST 但用于反应的对象。 (不想在我的反应应用程序中使用 jquery):
$.redirect(url + "external-entrance", model, "POST");
表单信息被发送给第三方,他们正在计算表单中的信息并在他们的网站上向我显示信息。在我当前的解决方案中,我使用 refs 提交表单。但是,我想使用状态,因为我可以更好地验证/屏蔽字段并将信息发送到我自己的后端。但是当我说我不能像ref="form"method="post"action="url"那样做一个帖子重定向时,使用axios post然后windows.location.href它只会把我带到链接但不会发布到它。
import React from 'react'
import axios from 'axios'
import { H3, P } from '../../components/StyledHeading'
import Button from '../../components/Button'
class ContactForm extends React.Component {
constructor(props) {
super(props)
this.state = {
firstName: '',
lastName: '',
email: '',
phone: '',
zipCode: '',
errors: [],
}
this.textInput = React.createRef();
}
_handleSubmit = evt => {
evt.preventDefault()
const payload = {
"first_name": "Thomas",
"last_name": "Edison",
"email": "edison@email.com",
"phone": "555-555-5355",
"zip_code": 239062,
}
this.refs.myForm.submit();
}
render() {
return (
<Container>
<form ref="myForm" method="post" action="https://externalsite.co/external-entrance">
<H3>Ready to reserve your spot?</H3>
<P className="subtitle">Fill out the form below and we’ll get in touch soon.</P>
<div className="inputFieldSection">
<label>
First Name:
<input
type="text"
ref={this.textInput}
disabled={isSending}
name="firstName"
placeholder="Thomas"/>
</label>
<label>
Last Name:
<input
type="text"
ref={this.textInput}
disabled={isSending}
name="lastName"
placeholder="Edison"/>
</label>
<label>
Phone Number:
<input
type="text"
ref={this.textInput}
disabled={isSending}
name="phoneNumber"
placeholder="555-555-5555"/>
</label>
<label>
Email address:
<input
type="text"
ref={this.textInput}
disabled={isSending}
name="emailAddress"
placeholder="edison@email.com"/>
</label>
<input type="hidden" ref={this.textInput} name="zipCode" value={29006}/>
</div>
<Button color={Color.secondaryColor} onClick={this._handleSubmit}>Submit My Reservation</Button>
</form>
</Container>
)
}
}
export default ContactForm
【问题讨论】: