简体   繁体   English

在React中阻止提交表单

[英]Resume prevented form submission in React

I'm implementing Stripe with React. 我正在用React实现Stripe。

Upon submit click, the form submission is prevented and an ajax request goes to Stripe - giving us a token in the response which should be attached in the subsequent request to our server. 在提交单击后,将阻止提交表单,并且将ajax请求发送到Stripe-在响应中给我们提供令牌,该令牌应在后续请求中附加到我们的服务器。

I'm stumbling on how to implement/trigger this subsequent request to our server? 我正在绊脚石如何实现/触发此后续请求到我们的服务器?

Below is an example of this flow taken from the react-stripe-elements repository: 以下是从react-stripe-elements存储库中获取的该流程的示例:

class _CardForm extends React.Component {
  props: {
    fontSize: string,
    stripe: StripeProps,
  }
  handleSubmit = (ev) => {
    ev.preventDefault();
    this.props.stripe.createToken().then((payload) => console.log(payload));
  }
  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Card details
          <CardElement
            onChange={handleChange}
            onFocus={handleFocus}
            onBlur={handleBlur}
            onReady={handleReady}
            {...createOptions(this.props.fontSize)}
          />
        </label>
        <button>Pay</button>
      </form>
    );
  }
}
const CardForm = injectStripe(_CardForm);

You should trigger your call to the server once you get your token, ie in the then clause of the promise that this.props.stripe.createToken returns. 获取令牌后,即应在this.props.stripe.createToken返回的this.props.stripe.createTokenthen子句中触发对服务器的调用。

handleSubmit = (ev) => {
  ev.preventDefault();
  this.props.stripe.createToken()
    .then((payload) => yourFetchImplementation('path/to/api/endpoint', payloadAsBody));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM