Axios doen';不要向php发送任何数据
我在Reactjs中使用axios将用户的数据发送到php,然后使用php创建一个新用户,但是当我想发送数据时,数据会变为空,php端会给出一些错误 我的反应码Axios doen';不要向php发送任何数据,php,reactjs,axios,Php,Reactjs,Axios,我在Reactjs中使用axios将用户的数据发送到php,然后使用php创建一个新用户,但是当我想发送数据时,数据会变为空,php端会给出一些错误 我的反应码 import React, { useReducer } from 'react'; import axios from 'axios'; import UserContext from './userContext'; import userReducer from './userReducer'; //Register use
import React, { useReducer } from 'react';
import axios from 'axios';
import UserContext from './userContext';
import userReducer from './userReducer';
//Register user
const registerUser = async () =>{
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
const response = await axios.post('http://localhost/react_cms/api/users/user.php' , {message : 'ppp'} , config);
console.log(response.data);
}
return (
<UserContext.Provider
value={{
registerUser
}}>
{props.children}
</UserContext.Provider>
);
}
export default UserState;
你要么改变这一行
$message = isset($_GET['message']) ? $_GET['message'] : 'no';
进入
或者保持原样,通过更改axios调用发送get请求
从
到
请注意,get
方法的签名不同于Axios中的post
方法,因为get
方法不需要数据参数
将数据发送到存储时使用POST动词被认为是一种良好的做法,而如果您正在检索某个标识符的数据,则可以使用GET
查看此文档以了解axios可以使用的http谓词的解释:。您似乎正在执行一个
POST
请求axios.POST()
,同时使用PHP读取$\u GET
数组以获取消息。在你的PHP文件中将$\u GET
更改为$\u POST
。我已经这样做了,但结果是一样的谢谢,正如你所说,我已经用GET方法做了,但它对POST不起作用method@mohammad不客气。也许值得尝试一下如何使用POST使其工作,因为您实际上是在发布数据,而不是检索数据。所以,在这里发布帖子是最理想的。
{message : 'no'}
$message = isset($_GET['message']) ? $_GET['message'] : 'no';
$message = isset($_POST['message']) ? $_POST['message'] : 'no';
const response = await axios.post('http://localhost/react_cms/api/users/user.php' , {message : 'ppp'} , config);
const response = await axios.get('http://localhost/react_cms/api/users/user.php?message=ppp', config);