ReactJS AXIOS post 不起作用
ReactJS AXIOS post doesn't work
我正在尝试向 API 提交登录表单,但是 API 没有通过 post 请求测试。
ReactJS 动作:
/**
* Sends login request to API
*/
export function login(email, password) {
console.log('Credentials:', email, password)
const request = axios.post(`${ROOT_URL}login/login`,
{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
}
来自 api 的登录功能:
public function actionLogin($credentials = [])
{
$request = Yii::$app->request;
if ($request->post()) {
// handle the user login
} else {
return Json::encode(['status' => false, 'data' => 'error_no_request']);
}
}
控制台记录响应 - 很明显它没有通过请求测试。
Headers:
Response Headers
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Length:42
Content-Type:text/html; charset=UTF-8
Date:Wed, 20 Sep 2017 06:42:06 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.8
X-Powered-By:PHP/7.0.8
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:56
Content-Type:application/json;charset=UTF-8
Host:127.0.0.1
Origin:http://localhost:8080
Referer:http://localhost:8080/login
控制台记录请求。
您正在将正文数据作为 null 传递。
编辑:
const request = axios.post(`${ROOT_URL}login/login`,
{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
收件人:
const request = axios.post(`${ROOT_URL}login/login`,
data:{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
Body 期望 data 关键字用于为 axios post 请求附加 json 对象的正文数据。
检查 documentation
好吧,既然POST方法不行,我就用GET方法试了一下。出于某种原因,它有效。操作现在看起来像:
export function login(email, password) {
const request = axios({
headers: {
'content-type': 'application/json'
},
method: 'post',
url: `${ROOT_URL}login/login`,
params: {
email,
password
}
})
.then((response) => response.data)
.catch((error) => error);
}
我正在尝试向 API 提交登录表单,但是 API 没有通过 post 请求测试。
ReactJS 动作:
/**
* Sends login request to API
*/
export function login(email, password) {
console.log('Credentials:', email, password)
const request = axios.post(`${ROOT_URL}login/login`,
{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
}
来自 api 的登录功能:
public function actionLogin($credentials = [])
{
$request = Yii::$app->request;
if ($request->post()) {
// handle the user login
} else {
return Json::encode(['status' => false, 'data' => 'error_no_request']);
}
}
Headers:
Response Headers
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Length:42
Content-Type:text/html; charset=UTF-8
Date:Wed, 20 Sep 2017 06:42:06 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.8
X-Powered-By:PHP/7.0.8
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:56
Content-Type:application/json;charset=UTF-8
Host:127.0.0.1
Origin:http://localhost:8080
Referer:http://localhost:8080/login
您正在将正文数据作为 null 传递。
编辑:
const request = axios.post(`${ROOT_URL}login/login`,
{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
收件人:
const request = axios.post(`${ROOT_URL}login/login`,
data:{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
Body 期望 data 关键字用于为 axios post 请求附加 json 对象的正文数据。 检查 documentation
好吧,既然POST方法不行,我就用GET方法试了一下。出于某种原因,它有效。操作现在看起来像:
export function login(email, password) {
const request = axios({
headers: {
'content-type': 'application/json'
},
method: 'post',
url: `${ROOT_URL}login/login`,
params: {
email,
password
}
})
.then((response) => response.data)
.catch((error) => error);
}