从浏览器端JavaScript到IBM MQ on Cloud的REST调用的“跨域请求被阻止” [英] “Cross-Origin Request Blocked” for REST calls to IBM MQ on Cloud from browser-side JavaScript

查看:151
本文介绍了从浏览器端JavaScript到IBM MQ on Cloud的REST调用的“跨域请求被阻止”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将REST调用发送到Cloud上的IBM MQ到队列中的PUT和GET消息。我想在浏览器端JavaScript中执行此操作,但收到跨域请求被阻止错误消息。
我的偏好是让客户端浏览器代码发送消息,但是我将解决我的服务器端Node.js代码发送消息的问题。

I am trying to send REST calls to IBM MQ on Cloud to PUT and GET messages from a queue. I want to do this in the browser-side JavaScript, but am receiving "Cross-Origin Request Blocked" error messages. My preference would be to have the client browser code send the message, but I will settle for my server-side Node.js code sending the message.

推荐答案

从浏览器直接向IBM MQ on Cloud发送消息

由于CORS被阻止,因此不可能,但是

This is not possible due CORS being blocked, but there is a workaround.

本质上,为了防止在用户无法控制的情况下滥用API,您的浏览器将阻止您访问其他站点的资源,除非该站点明确允许从您的站点访问它们网站,使用 Access-Control-Allow-Origin 标头(值 * 可用于允许来自所有站点)。

Essentially, to prevent misuse of APIs without the user’s control your browser will prevent you from accessing resources from another site, unless that site has specifically allowed access to them from your site, using the Access-Control-Allow-Origin header (a value of * can be used to allow access from all sites).

IBM MQ on Cloud的REST API不允许CORS,也不允许您修改任何Web Console或REST API属性,这意味着您无法向IBM发送消息直接通过浏览器访问MQ on Cloud。

IBM MQ on Cloud’s REST API does not allow CORS, nor does it allow you to modify any of the Web Console or REST API properties, meaning you cannot send messages to IBM MQ on Cloud directly from the browser.

从浏览器使用REST API的另一个原因是通常,这是一个坏主意,因为它鼓励开发人员在其浏览器端JavaScript代码中分发凭据,这是一种不良的安全做法。

Another reason why using the REST API from the browser is often a bad idea, is because it encourages developers to distribute credentials within their browser-side JavaScript code, which is a bad security practice.

此问题的解决方法是让浏览器客户端进行向您的服务器的请求,您将其配置为代表服务器将消息发送到IBM MQ on Cloud。这使您可以完全控制将什么消息发送到队列,这有其自身的好处。

The workaround to this is to have the browser clients make a request to your server, which you configure to send the messages to IBM MQ on Cloud on their behalf. This allows you to have full control over what messages are sent to the queue, which has its own benefits.

从Node.js服务器向IBM MQ on Cloud发送消息

您不妨使用 ibmmq npm软件包为您提供一切服务。 https://www.npmjs.com/package/ibmmq

You may wish to use the ibmmq npm package to take care of everything for you. https://www.npmjs.com/package/ibmmq

如果您不想使用该软件包,则可以使用自己喜欢的请求软件包,并通过 GET https://web-qmanager-example.qm.us之类的调用来调用API。 -south.mq.appdomain.cloud/ibmmq/rest/v1/login

If you do not wish to use that package, then you can use your favourite request package and call the API with calls such as GET https://web-qmanager-example.qm.us-south.mq.appdomain.cloud/ibmmq/rest/v1/login.

即使使用服务器,您仍然可能遇到CORS请求被阻止的错误消息的问题端Node.js代码。多年以来,以我的个人经验,我一直努力使jQuery在后端应用程序中运行以忽略CORS检查,此问题很可能已得到修复,但其他一些npm软件包可能也存在类似问题。
如果遇到这些问题,可以尝试以下几种方法。首先检查所用软件包的文档,可能需要跳过CORS检查选项。下一个选择是尝试使用其他程序包来处理REST调用。最后的选择是用另一种编程语言编写一个简单的代理,例如使用 http.server 的Python。

You may still run into problems with CORS request blocked error messages even with server-side Node.js code. In my personal experience many years ago, I struggled to get jQuery running in my backend app to ignore CORS checks, this has most probably since been fixed, but some other npm packages may have a similar issue. There are a couple of things to try if you hit these issues. First check the documentation for the package you are using, there may be a skip CORS check option that you need to enable. The next option is to try a different package for handling your REST calls. The final option would be to write a simple proxy in another programming language, such as in Python with http.server.

这篇关于从浏览器端JavaScript到IBM MQ on Cloud的REST调用的“跨域请求被阻止”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆