[英]ReactJS AXIOS post doesn't work
I'm trying to submit a login form to an API, however, the API doesn't pass the post request test. 我正在尝试向API提交登录表单,但API不会通过发布请求测试。
ReactJS Action: 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));
}
Login function from api: 来自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']);
}
}
Console logging the response - it's clear that it doesn't get through the request test.
控制台记录响应 - 很明显它没有通过请求测试。
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
Ok, so since the POST method doesn't work, I tried it with the GET method. 好吧,因为POST方法不起作用,我尝试使用GET方法。 And for some reason, it works.
出于某种原因,它有效。 The action now looks like:
现在的行动如下:
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);
}
You are passing body data as null . 您将正文数据传递为null 。
Edit : 编辑:
const request = axios.post(`${ROOT_URL}login/login`,
{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
To : 至 :
const request = axios.post(`${ROOT_URL}login/login`,
data:{
email,
password
})
.then((response) => console.log(response))
.catch((error) => console.log(error));
Body expects data keyword for the body data to which json object is attached for axios post request. Body期望为axios post请求附加json对象的正文数据的数据关键字。 Check the documentation
查看文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.