简体   繁体   English

Isomorphic React app - 当JS关闭时如何处理表单提交

[英]Isomorphic React app - how to handle form submission when JS is off

I have an isomorphic app that renders a sign in form using redux-form and allows a user to enter a user name and password. 我有一个同构应用程序,使用redux-form呈现登录表单,并允许用户输入用户名和密码。

This all works fine client-side but if I turn off JS I can't seem to get the values from the form to process via an API. 这一切都很好的客户端,但如果我关闭JS我似乎无法从表单中获取值来处理API。

NB: When form is GET, the values show in the url as query params but obviously don't want to show sensitive data. 注意:当表单是GET时,值在url中显示为查询参数,但显然不希望显示敏感数据。

class SignIn extends Component {

    static propTypes = {
        errorMessage: PropTypes.string,
        handleSubmit: PropTypes.func,
        signinUser: PropTypes.func
    }

    statics = {
        fields: [
            {
                name: 'email',
                label: 'Email'
            },
            {
                name: 'password',
                label: 'Password',
                type: 'password'
            }
        ]
    }

    handleFormSubmit({ email, password }) {
        this.props.signinUser({ email, password });
    }

    // renders an alert if field has an error
    renderAlert() {
        if (this.props.errorMessage) {
            return (
                <div className='alert alert-danger'>
                    <strong>Oops!</strong> {this.props.errorMessage}
                </div>
            );
        }

        return false;
    }

    // render the field
    renderField = ({ input, label, name, type, meta: { touched, error }, ...rest }) => (
        <div>
            <label htmlFor={name}>{label}</label>
            <div>
                <input {...input} {...rest} id={name} placeholder={label} type={type} />
                {touched && error && <span className='error'>{error}</span>}
            </div>
        </div>
    );

    render() {
        const { handleSubmit } = this.props;

        return (
            <form method='post' onSubmit={handleSubmit((data) => this.handleFormSubmit(data))}>
                {
                    this.statics.fields.map((field, index) =>
                        <fieldset className='form-group' key={index}>
                            <Field
                                className='form-control'
                                component={this.renderField}
                                label={field.label}
                                name={field.name}
                                type={field.type || 'text'}
                            />
                        </fieldset>
                    )
                }
                {this.renderAlert()}
                <button action='submit' className='btn btn-primary'>Sign in</button>
            </form>
        );
    }
}

and the routes: 和路线:

<Route path='/' component={App}>
    <IndexRoute component={Welcome} />
    <Route path='/feature' onEnter={requireAuth} component={Feature} />
    <Route path='/signin' component={Signin} />
    <Route path='/signout' component={Signout} />
    <Route path='/signup' component={Signup} />
</Route>

This is what https is for :) 这是https的用途:)

the page containing the form should be on https and it should be POSTing to a https end-point. 包含表单的页面应该在https上,它应该POST到https端点。

If you have well-written universal code and the page renders the form on the server (and all the JS is doing is enhancing the page) then the POSTing the form over https should be fine. 如果您有良好编写的通用代码并且页面在服务器上呈现表单(并且所有JS正在进行的是增强页面),则通过https发布表单应该没问题。

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

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