首页 \ 问答 \ ReactJS API数据获取CORS错误(ReactJS API Data Fetching CORS error)

ReactJS API数据获取CORS错误(ReactJS API Data Fetching CORS error)

我一直试图为我的实习创建一个反应web应用程序几天,我遇到了CORS错误。 我正在使用最新版本的reactJS,并将其放在create-react-app ,下面是获取的代码:

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的网址,但是,确认API后端没有CORS设置。

但是,在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上运行,则会出现以下错误

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

另一件事是我可以在我的浏览器中打开网址,没有任何问题或任何问题。

请帮助和谢谢!

附加信息

我添加CORS设置的原因是因为它提供了CORS错误,因此删除它并不能真正解决问题。

接下来我尝试执行代理设置,但现在它给出了

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

根据互联网,这是因为响应不是JSON。 但是,当我检查API时,它给出了这个

api img

这意味着返回类型应该是JSON吗?

附加信息

检查响应将产生这一点

{ “状态”:200, “总”:1, “per_page”:3 “CURRENT_PAGE”:1, “last_page”:1, “next_page_url”:NULL, “prev_page_url”:空 “从” 1” to“:3,”data“:[{”id“:1,”slug“:”america“,”name“:”america“,”address_1“:”USA Court“,”address_2“:”USA“, “城市”: “USA”, “状态”: “USA”, “POSTAL_CODE”: “94545”, “COUNTRY_CODE”: “美国”, “PHONE_NUMBER”: “10000001”, “CREATED_BY”:空, “updated_by”: null,“created_at”:“2017-11-10 11:30:50 + 00”,“updated_at”:“2018-06-28 07:27:55 + 00”}}}


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))
 }

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.

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).

and

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

If run on chrome it gives the following error

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

and

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.

and

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.

Please help and thanks!

Additional Information

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

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

which means that return type should be a JSON right?

Additional Info

checking the respond will yield this

{"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"}]}


原文:https://stackoverflow.com/questions/51128176
更新时间:2023-05-29 08:05

最满意答案

您可以为org.apache.activemq.artemis.core.server.impl.DivertImpl激活TRACE日志记录。 这将记录每个转移消息的消息。 请在此处查看日志记录代码


You can activate TRACE logging for org.apache.activemq.artemis.core.server.impl.DivertImpl. This will log a message for each diverted message. See the logging code here.

相关问答

更多
  • IBM将这些“活动日志”描述为知识中心页面“ 确保邮件不会丢失(日志记录) ”中的恢复日志 IBM MQ记录恢复日志中队列管理器控制的持久数据的所有重要更改。 这包括创建和删除对象, 持久性消息更新 ,事务状态,对象属性的更改以及通道活动。 该日志包含恢复消息队列的所有更新所需的信息: 保持队列管理器更改的记录 保留队列更新的记录以供重启过程使用 使您能够在硬件或软件发生故障后恢复数据 请注意, 非持久性消息不会记录到恢复日志中。 根据您的问题,您发送到外部系统的消息可能是持久性消息,而您从外部系统接收的消 ...
  • 似乎有一种方法可以通过API自定义地址安全性: ActiveMQServerControl 。 addSecuritySettings () Seems there is a way to customize an address security by API : ActiveMQServerControl.addSecuritySettings()
  • 您可以为org.apache.activemq.artemis.core.server.impl.DivertImpl激活TRACE日志记录。 这将记录每个转移消息的消息。 请在此处查看日志记录代码 You can activate TRACE logging for org.apache.activemq.artemis.core.server.impl.DivertImpl. This will log a message for each diverted message. See the loggi ...
  • 你与两个冲突的依赖关系有冲突。 以下依赖关系是针对ActiveMQ 5.x (!)的: org.apache.activemq activemq-broker Spring可能从该依赖项中找到JMS 1.0规范 ,该规范覆盖了类路径上的JMS 2.0规范,并在弹出窗口查找启动ConnectionFactory时使用的必需JMS接口时向您提供java. ...
  • 将消息发送到主题并为地址和队列启用自动创建时,只会创建地址而不是队列。 如果队列是自动创建的,并且消息被放入队列中,这将违反主题的语义。 仅在响应订户时创建主题地址上的订阅队列。 因此,在发送消息之前,您需要有关该主题的订阅者,否则将丢弃该消息(根据主题语义)。 When a message is sent to a topic and auto-creation is enabled for both addresses and queues only the address will be create ...
  • 我不能/不会谈论性能,但对简单的PTP和Pub / Sub用例来说应该不会太难。 RabbitMQ比JMS更灵活,因为它有很多交换类型,并且由于丰富的交换/绑定机制而使发布者和订阅者脱钩。 您可以使用消费者方面的消息选择器来获得某些功能。 Spring(主框架)在spring-jms jar中提供了一个JMS抽象 - 请参阅Spring Framework文档 。 I can't/won't speak to performance, but it shouldn't be too hard for sim ...
  • 请查看Artemis手册的管理章节 。 据我所知,您可以使用以下选项 JMX(Java管理服务) JMX是用于管理Java服务器的Java API。 有多个GUI通过JMX连接到Java服务器。 最值得注意的是jConsole。 这里或那里有一个其他GUI列表,或者您可以构建自己的 GUI JConsole的 jConsole是一个GUI应用程序,可以连接到启用JMX的服务器。 它是Java JDK的一部分,因此您可能已经安装了它。 椒 Jolokia ,是一个基于JMX的JSON API层。 它使JMX接 ...
  • artemis固有地支持使用诸如序列号之类的属性在队列内对消息进行重新排序。 在我看来,骆驼真的是最好的解决方案。 是否有可能在服务器内运行路由? 如果是的话,你可以举一个例子或给出文档的链接? 您应该能够在Artemis中使用具有Camel上下文的Web应用程序执行与ActiveMQ 5.x中相同的操作。 5.x doc就在这里 。 一些artemis功能(例如divert(拆分))需要修改代理配置(broker.xml文件),是否有办法以编程方式和动态方式执行它们,以便我可以决定何时开始转发消息? 您可 ...
  • 您可以尝试使用browse()操作。 对于filter参数,您需要指定属性值对,如JMSPriority=4 - > listMessages(JMSPriority=4) 不会。在订阅者持久之前,不会存储主题消息。 You can try with browse() operation. For filter parameter, you need to specify property-value pair like JMSPriority=4 -> listMessages(JMSPriority=4 ...
  • 似乎您现在的问题在这里讨论: http : //jira.codehaus.org/browse/JRUBY-672 尝试让你的Java类像这样: Position.java_proxy_class.java_class Seems like your current problem is discussed here: http://jira.codehaus.org/browse/JRUBY-672 try to get your Java class like this: Position.java_ ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。