javascript – ReactJS AXIOS帖子不起作用

我正在尝试向API提交登录表单,但API不会通过发布请求测试.

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

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方法.出于某种原因,它有效.现在的行动如下:

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帖子不起作用 - 代码日志