我有一个使用 json 数据生成的动态表单,我需要在提交时传递表单输入值。我打算将值作为表单数据发送。我已经创建了提交函数,但我不知道如何在 formdata 中附加值,并且需要使用 Axios 通过 post 方法。我是新手,谁能告诉我该怎么做。下面是我的代码。
var DATA = {
"indexList": [{
"Label": "Name",
"Type": "text",
"Regex": "",
"Default_Val": "",
"Values": {
"Key": "",
"Value": ""
},
"Validtion Msg": "",
"Script": "",
"Mandatory": "Y",
"maxLength":"16",
"minLength":"7",
"format":"Alphanumeric",
"cssClassName": "form-control",
"Placeholder": ""
},
{
"Label": "Select Language",
"Type": "dropdown",
"Regex": "",
"Default_Val": "English",
"Values": [{
"Key": "option1",
"Value": "English"
},{
"Key": "option2",
"Value": "Spanish"
}],
"Validtion Msg": "",
"Script": "",
"Mandatory": "Y",
"maxLength":"",
"minLength":"",
"format":"",
"cssClassName": "form-control",
"Placeholder": ""
},
{
"Label": "Type",
"Type": "radio",
"Regex": "",
"Default_Val": "",
"Values": [{
"Key": "option1",
"Value": "Form1"
}, {
"Key": "option2",
"Value": "Form2"
}, {
"Key": "option3",
"Value": "Form3"
},{
"Key": "option4",
"Value": "Form4"
},{
"Key": "option5",
"Value": "Form5"
}],
"Validtion Msg": "",
"Script": "",
"Mandatory": "Y",
"maxLength":"",
"minLength":"",
"format":"",
"cssClassName": "form-control",
"Placeholder": ""
}
]
};
var Menu = React.createClass({
getInitialState() {
return {
}
},
_renderElement: function(group){
switch(group.Type){
case 'text':
return (
<input
className={group.cssClassName}
id={group.Label}
placeholder={group.Placeholder}
required={group.Mandatory == 'Y' ? true : false}
/>
);
case 'dropdown':
return (
<select className={group.cssClassName}>
<option value="">{group.Placeholder}</option>
{group.Values.map(el => <option>{el.Value}</option>)}
</select>
);
case 'radio':
return (
<div className={group.Type}>
<div for="let value of group.Values">
{group.Values.map(el=> (
<label><input name="radios"/>{el.Value}</label>
))}
</div>
</div>
);
}
},
renderForm: function () {
var data = DATA.indexList;
return data.map(group => {
return (
<div>
<label for={group.Label}>{group.Label}</label>
<div>
{this._renderElement(group)}
</div>
</div>
)
});
},
_onSubmit: function () {
let formData = new FormData();
var data1 = DATA.indexList;
data1.map(group => {
formData.append(group.Label); // How to get the input value here as a second parameter, so than i can pass the label name and corresponding value to form data.
});
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
Axios.post('',formData, config)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
},
render: function() {
return (
<div className="container">
<br/>
<div className="panel panel-primary">
<div className="panel-heading">Form</div>
<div className="panel-body">
<form>
<div className="form-group">
<div className="col-xs-5">
{this.renderForm()}
<button type="submit" className="btn btn-primary" onSubmit={this._onSubmit()}>Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
)
}
});
ReactDOM.render(<Menu/>, document.getElementById('app'));
原文由 knbibin 发布,翻译遵循 CC BY-SA 4.0 许可协议
To post the
FormData
usingaxios
, you need specify inheader
that you are sendingFormData
, for thatcontent-type
应该是multipart/form-data
。检查这个,如何使用
FormData
和axios
: