Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 反应前端&x2B;WordPress API使用前端注册表创建/注册用户_Reactjs_Wordpress_Api - Fatal编程技术网

Reactjs 反应前端&x2B;WordPress API使用前端注册表创建/注册用户

Reactjs 反应前端&x2B;WordPress API使用前端注册表创建/注册用户,reactjs,wordpress,api,Reactjs,Wordpress,Api,我对React Axios的前端表单提交有问题。使用WordPress API通过前端注册表创建用户。表单有三个字段:用户名、电子邮件和密码。另外,我已经为令牌授权安装了JWT插件。当前错误为400错误请求。捕获错误消息为“缺少参数:密码” e.target看起来像什么?对不起,你是什么意思@jmargolisvtI的意思是将它添加到你的问题中。问题似乎是e.target[4]不存在。 const Register = ({ props }) => { const [register,

我对React Axios的前端表单提交有问题。使用WordPress API通过前端注册表创建用户。表单有三个字段:用户名、电子邮件和密码。另外,我已经为令牌授权安装了JWT插件。当前错误为400错误请求。捕获错误消息为“缺少参数:密码”


e.target
看起来像什么?对不起,你是什么意思@jmargolisvtI的意思是将它添加到你的问题中。问题似乎是e.target[4]不存在。
const Register = ({ props }) => {

const [register, setRegister] = useState({
    username: '',
    email: '',
    password: '',
    userCreated: false,
    loading: false,
    token: '',
    message: ''
   
})

const handleOnSubmit = e => {
    e.preventDefault()
    setRegister({ loading: true })
    console.log(register)

    const userData = { 
        username: e.target[0].value,
        email: e.target[2].value,
        password: e.target[4].value
        //role: 'Administrator'
    }

    const authToken = localStorage.getItem('token') 

    axios.post('http://localhost:8888/react/wp-json/wp/v2/users', userData, {

        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${authToken}`
        }
    })

        .then(res => {
            setRegister({
                loading: false,
                userCreated: !!res.data.id, 
                message: res.data.id ? 'New user created' : '',
            })
            console.log(res)
        })
    

        .catch(err => {
            setRegister({
                loading: false,
                message: err.response.data.message,
            })
            console.log(err)
        })

}


const handleOnChange = e => {
    setRegister({ ...register, [e.target.name]: e.target.value })
    console.log(register)
}



const { password, username, email, loading, error } = useState()

return ( my form here )