admin管理员组文章数量:1387301
I am Working on a React JS project. I have a separate local apache pdf file server. I am accessing any pdf file from url eg : 'http://local_server_IPaddress/dirname/sample.pdf'. I am passing this url to display pdf file in react ponent but I am getting below error. Please tell me how to solve this issue.
Ref- code
<Document
file="http://local_server_IPaddress/dirname/sample.pdf"
onLoadSuccess={this.onDocumentLoadSuccess}
>
<Page pageNumber={pageNumber} />
</Document>
Error log in browser console
Access to fetch at 'http://local_server_IPaddress/dirname/sample.pdf' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I am Working on a React JS project. I have a separate local apache pdf file server. I am accessing any pdf file from url eg : 'http://local_server_IPaddress/dirname/sample.pdf'. I am passing this url to display pdf file in react ponent but I am getting below error. Please tell me how to solve this issue.
Ref- https://www.npmjs./package/react-pdf code
<Document
file="http://local_server_IPaddress/dirname/sample.pdf"
onLoadSuccess={this.onDocumentLoadSuccess}
>
<Page pageNumber={pageNumber} />
</Document>
Error log in browser console
Access to fetch at 'http://local_server_IPaddress/dirname/sample.pdf' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Share Improve this question asked Dec 11, 2019 at 9:05 SUMIT VISHWAKARMASUMIT VISHWAKARMA 692 silver badges6 bronze badges 3-
1
does the server at
local_server_IPaddress
allow CORS? – Jaromanda X Commented Dec 11, 2019 at 9:07 - I don't know but I am fetching any files from url in local network. I am able to display images in my react ponent but not pdf file. it is throwing error – SUMIT VISHWAKARMA Commented Dec 11, 2019 at 9:11
- if the port is different it's a cross origin request – Jaromanda X Commented Dec 11, 2019 at 9:12
2 Answers
Reset to default 5Simple Solution Used for Development Purposes:
You can add this URL that allows cors before the actual URL
file={"https://cors-anywhere.herokuapp./" + pdf}
Add proxy
field into package.json
like this:
"proxy": "http://local_server_IPaddress"
Edit the code, and remove url of server like this:
<Document
file="/dirname/sample.pdf"
onLoadSuccess={this.onDocumentLoadSuccess}
>
<Page pageNumber={pageNumber} />
</Document>
It should works on build environment
See more: https://create-react-app.dev/docs/proxying-api-requests-in-development/
本文标签: javascriptError AccessControlAllowOrigin in React JS FrontendStack Overflow
版权声明:本文标题:javascript - Error Access-Control-Allow-Origin in React JS Frontend - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744503738a2609468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论