Javascript 使用react表单发布到API?
可以找到react表单的文档。我在定位URL的POST操作传递到库的位置和方式时遇到问题。我有一个API,它期望表单输入的值,但我似乎无法理解如何实际让组件发布到我指定的API端点 这是我的表单组件:Javascript 使用react表单发布到API?,javascript,forms,reactjs,Javascript,Forms,Reactjs,可以找到react表单的文档。我在定位URL的POST操作传递到库的位置和方式时遇到问题。我有一个API,它期望表单输入的值,但我似乎无法理解如何实际让组件发布到我指定的API端点 这是我的表单组件: import React, { Component } from 'react'; import { Form, Text, Select, Textarea } from 'react-form'; class DeploymentForm extends Component { re
import React, { Component } from 'react';
import { Form, Text, Select, Textarea } from 'react-form';
class DeploymentForm extends Component {
render() {
return (
<Form
onSubmit={(values) => {
console.log('Success!', values)
}}
validate={({ name }) => {
return {
name: !name ? 'A name is required' : undefined
}
}}
>
{({submitForm}) => {
return (
<div>
New STB Deployment
<form onSubmit={submitForm}>
<Text field='placeholder' placeholder='username'/>
<Text field='placeholder' placeholder='show'/>
<Text field='placeholder' placeholder='Git URL'/>
<Text field='placeholder' placeholder='Git Reference'/>
<Select
field='site'
options={[{
label: ''placeholder',
values: true
}]}
/>
<Select
field='Runway'
options={[{
label: 'Runway: stb',
values: true
}, {
label: 'Runway: stb2',
values: true
}, {
label: 'Runway: stb3',
values: true
}
]}
/>
<Select
field='Cluster: Default'
options={[{
label: 'placeholder',
values: true
}]}
/>
<Text field='hash' placeholder='placeholder' />
<Textarea
field='pre-deploy'
placeholder='placeholder'
<Textarea
field='post-deploy'
placeholder='placeholder'
/>
<Text field='placeholder' placeholder='placeholder'/>
<Text field='placeholder' placeholder='placeholder'/>
<button type='submit'>Submit</button>
</form>
</div>
)
}}
</Form>
)
}
}
export default DeploymentForm;
import React,{Component}来自'React';
从“react Form”导入{Form,Text,Select,Textarea};
类DeploymentForm扩展组件{
render(){
返回(
{
console.log('Success!',值)
}}
验证={({name})=>{
返回{
名称:!名称?“需要名称”:未定义
}
}}
>
{({submitForm})=>{
返回(
新机顶盒部署
您的render()
方法看起来很复杂。请尝试将逻辑置于render()
方法之外。发出post请求的更好方法如下所示:
class DeploymentForm extends Component {
constructor(props){
super(props);
this.state={'username': ''}
}
handleChange(e){
this.setState({username: e.target.value});
}
handleSubmit(e){
//call the api here with current state value (this.state.username)
}
render(){
return(
<form>
<input onChange={this.handleChange.bind(this)} type="text" name="username" placeholder="Enter name here" />
<button onClick={this.handleSubmit.bind(this)}>Submit</button>
</form>
)
}
}
class DeploymentForm扩展组件{
建造师(道具){
超级(道具);
this.state={'username':''}
}
手变(e){
this.setState({username:e.target.value});
}
handleSubmit(e){
//在此处使用当前状态值(this.state.username)调用api
}
render(){
返回(
提交
)
}
}
我对用于执行http请求的FetchAPI非常满意
例如:
var myInit = { method: 'POST',
headers: {},
body: dataVar};
fetch('http://API.com', myInit).then(function(response) {
return response.json();
}).then(function(jsonResponse) {
//Success message
console.log(jsonResponse);
}).catch(error){
console.log(error);
});
见参考资料:
react就像一种魅力。用承诺嵌套许多请求可能会有点烦人。很有趣。谢谢你给我关于我的混乱的建议。不过出于好奇,我仍然不知道如何将这些数据发布到URL端点。展示这样的例子会不会太麻烦?非常感谢对于您的时间,BTW.在AuthLeBuMIT中使用$.ajax POST方法将更容易。或者您也可以考虑使用AXIOS(总是使用异步的AsixOS等待ES2017)。有趣。我使用Axios处理HTTP请求,但从未处理过POST。因此,基本上,React不允许您指定表单操作/方法并以这种方式处理POST?为什么我需要自己在事件处理程序中处理此信息?表单提交本身就是一个事件。因此,我们的工作就是处理它。Axios只是帮助您处理此类请求精确地。