由于Rails / AJAX应用程序上的CORS,无法在本地访问IBM Watson API [英] Can't access IBM Watson API locally due to CORS on a Rails/AJAX App

查看:65
本文介绍了由于Rails / AJAX应用程序上的CORS,无法在本地访问IBM Watson API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何处理这个问题,似乎没有很多答案(但是有很多问题),所以我要在合唱中添加我的名字,并为不涉及的答案祈祷节点。

There doesn't seem to be a lot of answers (but lots of questions) out there on how to handle this, so I'm going to add my name to the chorus and pray for an answer that doesn't involve Node.

我通过Chrome控制台出现的错误:

My error via Chrome console:

1. POST https://gateway.watsonplatform.net/visual-recognition-beta/api 
2. XMLHttpRequest cannot load https://gateway.watsonplatform.net/visual-recognition-beta/api. 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 401.

我使用的是Rails AJAX请求,例如:

I'm using a Rails AJAX request as such:

$.ajax({
         method: "POST",
         version: 'v2-beta',
         url: "https://gateway.watsonplatform.net/visual-recognition-beta/api",
         password: "-----------",
         username: "-----------",
         version_date:'2015-12-02',
         visual_recognition: [
             {
             name: "visual-recognition-service",
             label: "visual_recognition",
             plan: "free",
             credentials: {
                 url: "https://gateway.watsonplatform.net/visual-recognition-beta/api",
                 password: "----------",
                 username: "---------"
               }
             }
           ],
         image: "/images/image1.jpg",
         contentType: 'application/json'
         }).done(function(msg){
         if (200) {
           console.log("This is a test for if.")
         } else {
           console.log("This is a test for else.")
         }
       });

对于这个特定的原型应用程序,我设置了Rack :: Cors以使任何东西都能工作。这是在我的application.rb中:

For this particular prototype app, I have Rack::Cors set up to let anything work. This is in my application.rb:

config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*',
        :headers => :any,
        :methods => [:get, :post, :delete, :put, :patch, :options, :head],
        :expose  => ['access-token', 'expiry', 'token-type', 'uid', 'client', 'auth-token'],
        :max_age => 0
      end
end

有没有人知道这些事情被配置为解决这个问题?我必须假设有一种无需启动Node实例即可访问这些API的方法。

Is there anyone out there that knows how these things are to be configured to get around this? I have to assume there's a way to access these APIs without having to fire up a Node instance.

推荐答案

以下服务支持< a href = https://en.wikipedia.org/wiki/Cross-origin_resource_sharing rel = nofollow noreferrer> CORS :


  • 语气分析器

  • 语音到文本

  • 文本到语音

  • 个性分析
  • 对话

  • Tone Analyzer
  • Speech to Text
  • Text to Speech
  • Personality Insights
  • Conversation

以下服务不支持CORS

The following services do not support CORS


  • 语言翻译

  • 视觉识别(部分支持)

  • 自然语言理解

我们正在努力增加对其余服务的支持。

We are working on adding support for the remaining services.

@ brian-martin建议,则不应在浏览器中使用凭据。您可以做的就是使用授权服务获取令牌,然后使用该令牌代替用户名和密码。查看有关如何使用令牌

As @brian-martin suggested, you should not use your credentials in the browser. Something you can do is to get a token using the authorization service and then use that token instead of username and password. Take a look at this tutorial on how to use tokens

更新04/07::添加了支持CORS的服务列表(感谢Nathan)
更新07/10::删除了不赞成使用的服务

UPDATE 04/07: Added list of services that support CORS(Thanks to Nathan) UPDATE 07/10: Removed deprecated services

这篇关于由于Rails / AJAX应用程序上的CORS,无法在本地访问IBM Watson API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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