【发布时间】:2016-06-30 06:35:03
【问题描述】:
关于如何处理这个问题似乎没有很多答案(但有很多问题),所以我将把我的名字添加到合唱团中,并祈祷一个不涉及的答案节点。
我通过 Chrome 控制台的错误:
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 请求:
$.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 中:
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
有没有人知道如何配置这些东西来解决这个问题?我必须假设有一种方法可以访问这些 API,而无需启动 Node 实例。
【问题讨论】:
-
这不是 Watson 问题。如需完整讨论,请参阅stackoverflow.com/questions/20035101/…
标签: ruby-on-rails ajax cors ibm-cloud ibm-watson