2017-09-20 2 views
2

API에 로그인 양식을 제출하려고하는데 API가 사후 요청 테스트를 통과하지 못합니다.ReactJS AXIOS 게시가 작동하지 않습니다.

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 콘솔.

+0

'API가 요청을받지 못했습니다.'그 사진이 무엇입니까? XMLHttpRequest가 200 응답 상태 인 것 같습니다. –

+0

잘못된 것 같습니다. 요청을 받지만 로그인 처리 부분에 도달하지 않습니다. –

답변

0

확인, 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); 
} 
0

신체 데이터를 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)); 

몸 Axios의 포스트 요청에 부착 된 JSON 객체에 대한 신체 데이터를 데이터 키워드를 기대하고있다. documentation

+0

여전히 작동하지 않습니다. 설명서를 확인하고 두 가지 방법으로 작동합니다. –

+0

네트워크 탭에 보내는 데이터가 무엇입니까? –

+0

@MoldovanAndrei 또한 text/html 대신 application/json으로 content-type 헤더를 설정하십시오. –

관련 문제