gpt4 book ai didi

javascript - ReactJS AXIOS 帖子不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:26 26 4
gpt4 key购买 nike

我正在尝试向 API 提交登录表单,但是该 API 未通过发布请求测试。

ReactJS Action :

/**
* 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']);
}
}

Console logging the request控制台记录响应 - 很明显它没有通过请求测试。

标题:

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

enter image description here控制台记录请求。

最佳答案

好的,既然POST方法不行,我就用GET方法试了一下。出于某种原因,它有效。 Action 现在看起来像:

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);
}

关于javascript - ReactJS AXIOS 帖子不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46314811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com