当前位置: 首页 > 面试题库 >

如何在Submit函数上的React.js中将表单值作为FormData传递

徐皓君
2023-03-14
问题内容

我有一个使用json数据生成的动态表单,我需要在提交时传递表单输入值。我打算将值作为formdata发送。我已经创建了Submit函数,但是我不知道如何在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'))

问题答案:

要发布FormData使用情况axios,您需要指定header要发送的内容FormData,因为content- type应该是multipart/form-data

勾选此,如何使用FormDataaxios

let formData = new FormData();    //formdata object

formData.append('name', 'ABC');   //append the values with key, value pair
formData.append('age', 20);

const config = {     
    headers: { 'content-type': 'multipart/form-data' }
}

axios.post(url, formData, config)
    .then(response => {
        console.log(response);
    })
    .catch(error => {
        console.log(error);
    });


 类似资料:
  • 问题内容: 在Java中,如何将一个函数作为另一个函数的参数传递? 问题答案: Java 8及以上 如果你的类或接口只有一个抽象方法(有时称为SAM type),则使用Java 8+ lambda表达式,例如: 然后可以在使用MyInterface的任何地方替换lambda表达式: 例如,你可以非常快速地创建一个新线程: 并使用方法引用语法使其更加清晰: 如果没有 lambda表达式,则最后两个示

  • 问题内容: 我该怎么做-在swift中传递两个NSStringDrawing选项作为函数参数: 问题答案: 编辑:在 Swift 3.0中 : 编辑: 这就是您将在Swift 2.0中使用选项枚举的方式 : 编辑: 此问题 已在iOS 8.3 SDK Beta 1(12F5027d)中得到解决 : 修改后的[struct] 从: 至: 您现在可以编写: 经过一番研究和@Anton Tcholako

  • 无法在react native expo应用程序中以“Multipart/formdata”内容类型发送表单数据。当我们在react-native应用程序的post请求中发送formData对象时,我们无法获取req。需求主体。来自节点后端的文件

  • 在Python中,我可以很容易地将函数作为参数传递,并在另一个函数内部执行。

  • 我正在尝试编写一个类模板,在内部它使用一个函数(BFGS优化的实现,由环境提供),接口如下: 其中< code>fn和< code>gr是类型的函数指针 和 分别是。我的< code>C 类模板如下所示: 这样,它可以由任何具有两个指定成员函数的类进行实例化,例如: 这可能吗?或者也许有更好的方法来做到这一点 - 将两个成员函数分别作为函数指针传递?(如果目标函数和相应的梯度很简单,那么编写两个函

  • 问题内容: 我有一个jQuery ajax函数,想提交一个完整的表单作为发布数据。我们正在不断更新表单,因此不断更新应该在请求中发送的表单输入变得很乏味。 问题答案: 有一个函数可以做到这一点: http://api.jquery.com/serialize/