localhost에서 실행중인 서버 (즉, /의 디렉토리 목록)에서 자원을 페치 (fetch)하려고하는 파일 시스템에서 정적 페이지를 엽니 다. 브라우저는이 요청을 전달하기를 거부하고 제안 : 나는 다음과 같은 방법으로 명시 사용하는 서버 측에서'no-cors'모드에서 리소스를 가져올 때 body-parser가 실패하는 이유는 무엇입니까?
let body=JSON.stringify({action:"listfiles",path:"/"})
let headers = new Headers()
headers.append("Content-Type", "application/json");
fetch('http://localhost:9000/ajax',{
method: 'POST',
headers: headers,
body: body,
mode: 'no-cors'
}).then(
response=>response.json()
).then(
data=>this.onload(data)
)
:
그런 다음If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
나는 '노 고르'를 사용하지 않는 요청을 제출 :
app.use('/ajax',bodyParser.json())
app.post("/ajax",function(req,res){
let action=req.body.action
console.log(`ajax ${action}`)
}
브라우저를 통해 요청을 할 수 있습니다, 심지어 app.post
그냥 요청 본문 corre 구문 분석되지 않으며, 요청을 얻을이 시간 ctly 대신 'listfiles'
action
에 대한 undefined
값을 얻습니다. (localhost에서 페이지를로드해도 같은 문제가 발생하지 않습니다.)
https://stackoverflow.com/questions/42254685/text-response-is-empty-when-using-fetch/42255007#42255007 및 https://stackoverflow.com/questions/43317967/handle-response- syntaxerror-unexpected-end-of-input/43319482 # 43319482. 당신은 기본적으로 "mode : 'no-cors'"를 사용하고 싶지 않습니다. 브라우저가 프론트 엔드 JavaScript가 응답 본문과 헤더에 액세스하는 것을 완전히 차단합니다. – sideshowbarker