简体   繁体   English

网络错误/403 Forbidden on request to a service React JS axios 代理

[英]Network Error / 403 Forbidden on request to a service React JS axios proxy

I need to make a React app that makes a request to this service https://www.indecon.online/last for a Job application and I'm currently having an issue when trying to make a GET request.我需要制作一个 React 应用程序,该应用程序向此服务https://www.indecon.online/last提出工作申请,我目前在尝试发出 GET 请求时遇到问题。

It works fine browsers or inside Postman, but when trying with axios its not the same story.它可以在浏览器或 Postman 中正常工作,但在尝试使用 axios 时,情况就不一样了。

At first I tried to make a simple GET request with axios like so:起初,我尝试使用 axios 发出一个简单的 GET 请求,如下所示:

在此处输入图像描述

and I got back this error:我得到了这个错误:

Access to XMLHttpRequest at 'https://www.indecon.online/last' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

So, I tried a second time, this time using http-proxy-middleware npm package (following this tutorial: https://levelup.gitconnected.com/overview-of-proxy-server-and-how-we-use-them-in-react-bf67c062b929 ). So, I tried a second time, this time using http-proxy-middleware npm package (following this tutorial: https://levelup.gitconnected.com/overview-of-proxy-server-and-how-we-use-them -in-react-bf67c062b929 )。 I configured a setupProxy.js file like so:我配置了一个 setupProxy.js 文件,如下所示:

在此处输入图像描述

And simply changed the async function like this:并像这样简单地更改异步 function :

在此处输入图像描述

And now I'm getting this error after the request (second one is the result of a console.error() inside the catch block):现在我在请求之后收到了这个错误(第二个是 catch 块内的 console.error() 的结果):

GET http://localhost:3000/last 403 (Forbidden)

ERROR:  Error: Request failed with status code 403
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)

I have already tried the proxy config and the axios request with another public service and got a successful response.我已经用另一个公共服务尝试了代理配置和 axios 请求并得到了成功的响应。

I appreciate any help!我很感激任何帮助!

I assume you created the app with create-react-app so you have a package.json that has a dependency to react-scripts and a start script using react-scripts.我假设您使用 create-react-app 创建了应用程序,因此您有一个 package.json 依赖于 react-scripts 和使用 react-scripts 的启动脚本。 You can remove the http-proxy-middleware package from your project and add proxy entry in package.json:您可以从项目中删除 http-proxy-middleware package 并在 package.json 中添加代理条目:

{
  "dependencies": {
    "react-scripts": "3.4.1", <-- you should already have this
  },
  "scripts": {
    "start": "react-scripts start", <-- you should already have this
  },
  "proxy": "https://www.indecon.online"
}

Now in your react code you can do axios.get('/last')现在在您的反应代码中,您可以执行axios.get('/last')

Start your project with npm or yarn start.使用 npm 或 yarn start 开始您的项目。

If it doesn't work then can you please post your package.json in the question (not an image but copy paste the text please).如果它不起作用,那么您能否在问题中发布您的 package.json(不是图像,但请复制粘贴文本)。

This answer is also in the documentation you refer to in your question.该答案也在您在问题中引用的文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM