pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<tfoot id='cTHl6'></tfoot>
    <legend id='cTHl6'><style id='cTHl6'><dir id='cTHl6'><q id='cTHl6'></q></dir></style></legend>

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

      1. <i id='cTHl6'><tr id='cTHl6'><dt id='cTHl6'><q id='cTHl6'><span id='cTHl6'><b id='cTHl6'><form id='cTHl6'><ins id='cTHl6'></ins><ul id='cTHl6'></ul><sub id='cTHl6'></sub></form><legend id='cTHl6'></legend><bdo id='cTHl6'><pre id='cTHl6'><center id='cTHl6'></center></pre></bdo></b><th id='cTHl6'></th></span></q></dt></tr></i><div class="5x5555x" id='cTHl6'><tfoot id='cTHl6'></tfoot><dl id='cTHl6'><fieldset id='cTHl6'></fieldset></dl></div>
        • <bdo id='cTHl6'></bdo><ul id='cTHl6'></ul>
      2. ReactJS API 數據獲取 CORS 錯誤

        ReactJS API Data Fetching CORS error(ReactJS API 數據獲取 CORS 錯誤)
          <tbody id='TKMrB'></tbody>
        • <bdo id='TKMrB'></bdo><ul id='TKMrB'></ul>

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

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

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

                • <tfoot id='TKMrB'></tfoot>

                  本文介紹了ReactJS API 數據獲取 CORS 錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  幾天來,我一直在為我的實習嘗試創建一個 React Web 應用程序,但遇到了 CORS 錯誤.我正在使用最新版本的 reactJS,并將其放在 create-react-app 中,下面是獲取代碼:

                  I've been trying to create a react web app for a few days now for my internship and I've encountered a CORS error. I am using the latest version of reactJS, and placing this in the create-react-app, and below is the code for fetching:

                  componentDidMount() {
                    fetch('--------------------------------------------',{
                      method: "GET",
                      headers: {
                        "access-control-allow-origin" : "*",
                        "Content-type": "application/json; charset=UTF-8"
                      }})
                    .then(results => results.json())
                    .then(info => {
                      const results = info.data.map(x => {
                        return {
                          id: x.id,
                          slug: x.slug,
                          name: x.name,
                          address_1: x.address_1,
                          address_2: x.address_2,
                          city: x.city,
                          state: x.state,
                          postal_code: x.postal_code,
                          country_code: x.country_code,
                          phone_number: x.phone_number,
                        }
                      })
                      this.setState({warehouses: results, lastPage: info.last_page});
                    })
                    .then(console.log(this.state.warehouses))
                   }
                  

                  很抱歉,由于公司規定,我不能發布API的url,但是,已經確認API后端沒有CORS設置.

                  I'm sorry that I can't post the url for the API due to company rules, however, it is confirmed that there are no CORS setting in the API backend.

                  但是,我在 mozilla 上運行時遇到以下錯誤

                  However, I encounter the following errors when run on mozilla

                  Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ------------------. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
                  

                  Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ---------------------------------------------. (Reason: CORS request did not succeed).
                  

                  如果在 chrome 上運行,則會出現以下錯誤

                  If run on chrome it gives the following error

                  Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
                  

                  Failed to load --------------------------------------------------------: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
                  

                  Uncaught (in promise) TypeError: Failed to fetch
                  

                  另一件事是,我可以在瀏覽器中打開網址,沒有任何問題.

                  Another thing is that I am able to open the url in my browsers with no problems or whatsoever.

                  請幫忙,謝謝!

                  其他信息

                  我添加 CORS 設置的原因是它給出了 CORS 錯誤,因此刪除它并不能真正解決問題.

                  The reason I added the CORS setting is because it gives a CORS error, so removing it does not really solve the issue.

                  接下來我嘗試進行代理設置,但是,它現在給出了

                  Next I tried to perform proxy setting, however, it now gives

                  Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0
                  

                  根據互聯網,這是因為響應不是 JSON.但是,當我檢查 API 時,它給出了這個

                  According to the internet this is caused becasue the response is not a JSON. However when I checked the API it gives this

                  api img

                  這意味著返回類型應該是 JSON 對嗎?

                  which means that return type should be a JSON right?

                  其他信息

                  檢查響應將產生這個

                  {"status":200,"total":1,"per_page":3,"current_page":1,"last_page":1,"next_page_url":null,"prev_page_url":null,"from":1,"to":3,"data":[{"id":1,"slug":"america","name":"america","address_1":"美國法院","address_2":"美國","city":"USA","state":"USA","postal_code":"94545","country_code":"US","phone_number":"10000001","created_by":null,"updated_by":null,"created_at":"2017-11-10 11:30:50+00","updated_at":"2018-06-28 07:27:55+00"}]}

                  {"status":200,"total":1,"per_page":3,"current_page":1,"last_page":1,"next_page_url":null,"prev_page_url":null,"from":1,"to":3,"data":[{"id":1,"slug":"america","name":"america","address_1":"USA Court","address_2":"USA","city":"USA","state":"USA","postal_code":"94545","country_code":"US","phone_number":"10000001","created_by":null,"updated_by":null,"created_at":"2017-11-10 11:30:50+00","updated_at":"2018-06-28 07:27:55+00"}]}

                  推薦答案

                  需要在 API 中設置 CORS 設置以允許從您的 React 應用程序域進行訪問.沒有 CORS 設置,沒有來自不同域的 AJAX.就這么簡單.您可以將 CORS 設置添加到您的公司 API(這不太可能發生),也可以按如下所述解決:

                  The CORS settings need to be setup in the API to allow access from your React app domain. No CORS settings, no AJAX from different domains. It's simple as that. You can either add CORS settings to your company API (this is unlikely to happen) or you can work around like described below:

                  CORS 只是客戶端瀏覽器保護用戶免受惡意 AJAX 攻擊的一種機制.因此,解決此問題的一種方法是將您的 AJAX 請求從您的 React 應用程序代理到它自己的 Web 服務器.正如文森特建議的那樣,create-react-app 提供了一種簡單的方法來做到這一點:在您的 package.json 文件中,只需簡單地夾住 "proxy": "http://your-company-api-domain".有關更多詳細信息,請參閱此 鏈接

                  The CORS is solely a mechanism of client browser to protect users from malicious AJAX. So one way to work around this is proxying your AJAX request from your React app to its own web server. As Vincent suggests, the create-react-app provides an easy way to do this: in your package.json file, simply chuck "proxy": "http://your-company-api-domain". For more details, please see this link

                  然后在您的反應應用程序中,您可以使用這樣的相對 URL:fetch('/api/endpoints').請注意,相對 URL 必須與您的公司 API 匹配.這將向您的服務器發送請求,然后服務器會將請求轉發到您的公司 API 并將響應返回給您的應用程序.由于請求是以服務器到服務器的方式而不是瀏覽器到服務器的方式處理的,因此不會發生 CORS 檢查.因此,您可以去掉請求中所有不必要的 CORS 標頭.

                  Then in your react app you can using relative URL like this: fetch('/api/endpoints'). Notice that the relative URL has to match with your company API. This will send a request to your server, then the server will forward the request to your company API and return the response back to your app. Since the request is handled in the server-to-server way not browser-to-server so the CORS check won't happen. Therefore, you can get rid of all unnecessary CORS headers in your request.

                  這篇關于ReactJS API 數據獲取 CORS 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)

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

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

                      <tbody id='QcrPC'></tbody>

                    <legend id='QcrPC'><style id='QcrPC'><dir id='QcrPC'><q id='QcrPC'></q></dir></style></legend>
                      • <bdo id='QcrPC'></bdo><ul id='QcrPC'></ul>

                            主站蜘蛛池模板: 锥形螺带干燥机(新型耙式干燥机)百科-常州丰能干燥工程 | 邢台人才网_邢台招聘网_邢台123招聘【智达人才网】 | 天津蒸汽/热水锅炉-电锅炉安装维修直销厂家-天津鑫淼暖通设备有限公司 | 深圳VI设计-画册设计-LOGO设计-包装设计-品牌策划公司-[智睿画册设计公司] | 消泡剂_水处理消泡剂_切削液消泡剂_涂料消泡剂_有机硅消泡剂_广州中万新材料生产厂家 | 食品级焦亚硫酸钠_工业级焦亚硫酸钠_焦亚硫酸钠-潍坊邦华化工有限公司 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 深圳品牌设计公司-LOGO设计公司-VI设计公司-未壳创意 | 液压油缸-液压站生产厂家-洛阳泰诺液压科技有限公司 | 磁粉制动器|张力控制器|气胀轴|伺服纠偏控制器整套厂家--台灵机电官网 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 耐酸碱胶管_耐腐蚀软管总成_化学品输送软管_漯河利通液压科技耐油耐磨喷砂软管|耐腐蚀化学软管 | 寮步纸箱厂_东莞纸箱厂 _东莞纸箱加工厂-东莞市寮步恒辉纸制品厂 | 杰福伦_磁致伸缩位移传感器_线性位移传感器-意大利GEFRAN杰福伦-河南赉威液压科技有限公司 | 气力输送设备_料封泵_仓泵_散装机_气化板_压力释放阀-河南锐驰机械设备有限公司 | 上海乾拓贸易有限公司-日本SMC电磁阀_德国FESTO电磁阀_德国FESTO气缸 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 杭州中央空调维修_冷却塔/新风机柜/热水器/锅炉除垢清洗_除垢剂_风机盘管_冷凝器清洗-杭州亿诺能源有限公司 | 合肥网络推广_合肥SEO网站优化-安徽沃龙First | 空气弹簧|橡胶气囊|橡胶空气弹簧-上海松夏减震器有限公司 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 聚丙烯酰胺PAM-聚合氯化铝PAC-絮凝剂-河南博旭环保科技有限公司 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 强效碱性清洗剂-实验室中性清洗剂-食品级高纯氮气发生器-上海润榕科学器材有限公司 | 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 牛奶检测仪-乳成分分析仪-北京海谊| 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 半自动预灌装机,卡式瓶灌装机,注射器灌装机,给药器灌装机,大输液灌装机,西林瓶灌装机-长沙一星制药机械有限公司 | 细沙回收机-尾矿干排脱水筛设备-泥石分离机-建筑垃圾分拣机厂家-青州冠诚重工机械有限公司 | 冷却塔降噪隔音_冷却塔噪声治理_冷却塔噪音处理厂家-广东康明冷却塔降噪厂家 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 | 展厅设计公司,展厅公司,展厅设计,展厅施工,展厅装修,企业展厅,展馆设计公司-深圳广州展厅设计公司 | 小青瓦丨古建筑瓦丨青瓦厂家-宜兴市徽派古典建筑材料有限公司 | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! |