2017-12-04 3 views
0

반응 프런트 엔드에서 PHP 파일로 게시물 요청을 보내려고합니다. POST 가져 오기 요청 - JSON으로 변환

handleFormSubmit(event) { 
    event.preventDefault() 
    let payload = this.state.email; 
    fetch(`http://website.com/file.php`, { 
    credentials: 'same-origin', 
    method: 'POST', 
    mode: 'cors', 
    headers:{ 
     'Access-Control-Allow-Origin':'*' 
     }, 
    body: payload 
    }) 
    .then((response) => response.json()) 
    .then((body) => { 
    console.log(body); 
    this.setState({ 
     email: body, 
     message: 'Success!' 
    }); 
    }); 
} 

나는 페치 요청이 통과하는 대신 오류 메시지가 무엇입니까. 서버가 프리 플라이트 요청 (옵션)에 대한 응답으로 제어 - 액세스 허용 - - 원산지 헤더를 포함하지 않는 것처럼

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

+0

프리 플라이트 (옵션) 통화를 위해 끝점에 CORS 문제가 있습니다. – ajmajmajma

답변

0

보인다. Access-Control-Allow-Origin이없는 경우 CORS 요청이 실패합니다. 이 도움이

https://www.html5rocks.com/en/tutorials/cors/

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

희망 : 여기

은 CORS가 작동하는 방법을 설명하는 몇 가지 좋은 기사입니다!

+0

감사합니다! 그것은 그것을 설명합니다. – lemonyellow

+0

cors 대신 no-cors 사용 @lemonyellow –

+0

'cors'를'no-cors'로 변경하면 다음과 같은 오류 메시지가 나타납니다 : '잡히지 않습니다 (약속 있음) SyntaxError : 예상치 못한 입력 끝'. 그 오류의 원인은 무엇입니까? – lemonyellow

관련 문제