<i id='wa7XQ'><tr id='wa7XQ'><dt id='wa7XQ'><q id='wa7XQ'><span id='wa7XQ'><b id='wa7XQ'><form id='wa7XQ'><ins id='wa7XQ'></ins><ul id='wa7XQ'></ul><sub id='wa7XQ'></sub></form><legend id='wa7XQ'></legend><bdo id='wa7XQ'><pre id='wa7XQ'><center id='wa7XQ'></center></pre></bdo></b><th id='wa7XQ'></th></span></q></dt></tr></i><div id='wa7XQ'><tfoot id='wa7XQ'></tfoot><dl id='wa7XQ'><fieldset id='wa7XQ'></fieldset></dl></div>

      <small id='wa7XQ'></small><noframes id='wa7XQ'>

      <legend id='wa7XQ'><style id='wa7XQ'><dir id='wa7XQ'><q id='wa7XQ'></q></dir></style></legend>
    1. <tfoot id='wa7XQ'></tfoot>

        <bdo id='wa7XQ'></bdo><ul id='wa7XQ'></ul>

      1. 从 React(同构应用程序)进行 API 调用时出现“Acc

        时间:2023-09-03
      2. <legend id='Q2YXa'><style id='Q2YXa'><dir id='Q2YXa'><q id='Q2YXa'></q></dir></style></legend>

        <small id='Q2YXa'></small><noframes id='Q2YXa'>

        • <i id='Q2YXa'><tr id='Q2YXa'><dt id='Q2YXa'><q id='Q2YXa'><span id='Q2YXa'><b id='Q2YXa'><form id='Q2YXa'><ins id='Q2YXa'></ins><ul id='Q2YXa'></ul><sub id='Q2YXa'></sub></form><legend id='Q2YXa'></legend><bdo id='Q2YXa'><pre id='Q2YXa'><center id='Q2YXa'></center></pre></bdo></b><th id='Q2YXa'></th></span></q></dt></tr></i><div id='Q2YXa'><tfoot id='Q2YXa'></tfoot><dl id='Q2YXa'><fieldset id='Q2YXa'></fieldset></dl></div>
              <tbody id='Q2YXa'></tbody>
              <bdo id='Q2YXa'></bdo><ul id='Q2YXa'></ul>
            • <tfoot id='Q2YXa'></tfoot>

                • 本文介绍了从 React(同构应用程序)进行 API 调用时出现“Access-Control-Allow-Origin"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在使用 React 和 Express 的同构 JavaScript 应用程序时遇到了问题.

                  I'm running into an issue with my isomorphic JavaScript app using React and Express.

                  我正在尝试在我的组件挂载时使用 axios.get 发出 HTTP 请求

                  I am trying to make an HTTP request with axios.get when my component mounts

                  componentDidMount() {
                    const url = 'http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders';
                    axios.get(url).then( res => {
                      //use res to update current state
                    })
                  }
                  

                  我从 API 获得状态 200 res,但我没有获得任何响应数据并且在我的控制台中收到错误

                  I am getting a status 200 res from the API, but I am not getting any response data and getting an error in my console

                  XMLHttpRequest cannot load http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders. 
                  No 'Access-Control-Allow-Origin' header is present on the requested resource. 
                  Origin 'http://localhost:3000' is therefore not allowed access.
                  

                  但是,如果我在 server.js 中提出请求

                  However, if I make the request in my server.js

                  const url = 'http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders';
                  axios.get(url).then(res => {
                      //console.log(res);
                  });
                  

                  它工作正常,我在服务器启动时得到响应数据.这是实际 API 的问题还是我做错了什么?如果这是一个 CORS 问题,我猜 server.js 中的请求也不起作用?谢谢!

                  It works fine and I get response data when the server starts. Is this an issue with the actual API or am I doing something wrong? If this was a CORS issue I'm guessing the request in server.js wouldn't work either? Thanks!

                  推荐答案

                  CORS 是一个浏览器功能.服务器需要选择加入 CORS 以允许浏览器绕过同源策略.您的服务器将没有相同的限制,并且能够使用公共 API 向任何服务器发出请求.https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

                  CORS is a browser feature. Servers need to opt into CORS to allow browsers to bypass same-origin policy. Your server would not have that same restriction and be able to make requests to any server with a public API. https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

                  在您的服务器上创建一个启用了 CORS 的端点,该端点可以充当您的 Web 应用程序的代理.

                  Create an endpoint on your server with CORS enabled that can act as a proxy for your web app.

                  这篇关于从 React(同构应用程序)进行 API 调用时出现“Access-Control-Allow-Origin"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:请求的资源上没有“Access-Control-Allow-Origin"标 下一篇:跨域资源共享 GET:'拒绝获取不安全的标头“

                  相关文章

                  最新文章

                • <small id='Kvoa2'></small><noframes id='Kvoa2'>

                    <i id='Kvoa2'><tr id='Kvoa2'><dt id='Kvoa2'><q id='Kvoa2'><span id='Kvoa2'><b id='Kvoa2'><form id='Kvoa2'><ins id='Kvoa2'></ins><ul id='Kvoa2'></ul><sub id='Kvoa2'></sub></form><legend id='Kvoa2'></legend><bdo id='Kvoa2'><pre id='Kvoa2'><center id='Kvoa2'></center></pre></bdo></b><th id='Kvoa2'></th></span></q></dt></tr></i><div id='Kvoa2'><tfoot id='Kvoa2'></tfoot><dl id='Kvoa2'><fieldset id='Kvoa2'></fieldset></dl></div>
                      • <bdo id='Kvoa2'></bdo><ul id='Kvoa2'></ul>
                    1. <legend id='Kvoa2'><style id='Kvoa2'><dir id='Kvoa2'><q id='Kvoa2'></q></dir></style></legend>
                      <tfoot id='Kvoa2'></tfoot>