没有在带有redux的反应js中使用axios发送一个post请求(在post请求中没有发送任何内容)

时间:2018-04-03 03:03:36

标签: reactjs redux react-redux axios redux-form

我的问题是我要发布一个带有axios的帖子请求,需要将表单发送到后端,但不发送任何用于发布请求,我使用redux表单捕获和axios发送表单,并在烧瓶中后端。 我看到了另一个答案但是没有用。

我正在使用redux作为应用程序的状态。

这是redux-form代码:

    import React from 'react'
    import {Field,reduxForm} from 'redux-form'
    import {connect} from 'react-redux'
    import { showResults,showOneRestaurante}from '../../actions/actions'
    const SimpleForm=props=>{

      const {handleSubmit}=props;
      const onFormSubmit=(data)=>{            
        let id=data.idRestaurante;           
        var formData = new FormData();         

        formData.append('idRestaurante',id)           
        const config={
          headers: { 
            "Content-Type": "application/x-www-form-urlencoded"
          }           
        }

        showOneRestaurante(formData,config)
      }
      return (
        <form onSubmit={handleSubmit(onFormSubmit)}>
            <div>
                <label> Id Restaurante </label>
                      <Field
                          name='idRestaurante'
                          component='input'
                          type='text'
                          placeholder='ID  RESTAURANTE'
                      />               

            </div>        
            <button type='submit' > Submit </button>
        </form>
      )
    }

    export default connect(null)( reduxForm({
      form:'idRestautanres'
    })(SimpleForm));

这是使用axios代码的行动:

            import React from 'react'
            import axios from 'axios'                               
            import renderRestaurantesList from '../components/restaurantesLayout/allRestaurantes'
            import qs from 'qs'                
            export const SHOW_ONE_RESTAURANTE='SHOW_ONE_RESTAURANTE';          

            export function showOneRestaurante(formData,config){               
                return (dispatch)=>(                                                                 
                    axios({
                        method:'post',
                        url:"http://127.0.0.1:5000/obtenerRestaurante",                            
                        data:qs.stringify( formData)
                    }                        
                )
                    .then(response=>{
                        console.log('showone')
                        console.log(response)
                        dispatch({type:SHOW_ONE_RESTAURANTE,payload:renderRestaurantesList(response.data)})                            
                    })
                    .catch(function(err){
                        dispatch(<div>mal</div>)
                    })
                )   
            }

在最后一段代码中,如果我使用数据:qs.stringify({idRestaurante:'4'})它的工作原理如下:

 axios({method:'post',
       url:"http://127.0.0.1:5000/obtenerRestaurante",
       data:qs.stringify({idRestaurante:'4'})
 }

改为使用:      Axios公司({方法: '后',            网址: “http://127.0.0.1:5000/obtenerRestaurante”,            data:qs.stringify(formData)      } 但是很明显我需要使用表单的特定数据,在我的后端我使用烧瓶,并获得表单信息,我写下烧瓶中的下一个代码:

def GetRestaurante():

    if request.method=="POST":


        PidRestaurante = request.form['idRestaurante']

后面使用邮递员正常工作,正如我之前所说,在我的前端,只有当我使用代码qs.stringify({idRestaurante:'4'})时才能正确显示信息。

这是我在堆栈溢出中的第一个问题,我很快就会很感激答案:)。

1 个答案:

答案 0 :(得分:0)

const onFormSubmit=(data)=>{            
        data = { id: data.idRestaurante };
        const config={
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          }           
        }

        showOneRestaurante(data,config)
      }
showOneRestaurante中的

axios({
       method:'post',
       url:"http://127.0.0.1:5000/obtenerRestaurante",
       data:qs.stringify(data),
 })