Microsoft 认知服务 JavaScript 请求 'Access-Control-Allow-Origin'
Microsoft Cognitive Services JavaScript Request 'Access-Control-Allow-Origin'
嗨,当我尝试从我的 server/local 机器上通过 JavaScript 访问 Microsoft Cognitive Services API 时,出现以下错误。
XMLHttpRequest cannot load http://api.projectoxford.ai/vision/v1.0/analyze?visualFeatures=Categories. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myUrl.com' is therefore not allowed access. The response had HTTP status code 401.
这是我的请求代码:
function requestAPI(){
var params = {
// Request parameters
"visualFeatures": "Categories"
};
$.ajax({
url: "http://api.projectoxford.ai/vision/v1.0/analyze?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","{myKey}");
},
type: "POST",
// Request body
data: "{'url':'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg'}",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
}
在我的 .htaccess 中我已经添加了:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
当我使用 hurl.it 测试请求时,它有效。就我的服务器而言,它没有。
看起来您正在为您的服务器设置 CORS headers,这意味着有人可以向您的服务器发出 cross-domain 请求。
Microsoft Cognitive Services 必须在他们的服务器上添加这些 headers 以便您可以向他们发出 cross-domain 请求,或者您必须使用 JSONP.
嗨,当我尝试从我的 server/local 机器上通过 JavaScript 访问 Microsoft Cognitive Services API 时,出现以下错误。
XMLHttpRequest cannot load http://api.projectoxford.ai/vision/v1.0/analyze?visualFeatures=Categories. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myUrl.com' is therefore not allowed access. The response had HTTP status code 401.
这是我的请求代码:
function requestAPI(){
var params = {
// Request parameters
"visualFeatures": "Categories"
};
$.ajax({
url: "http://api.projectoxford.ai/vision/v1.0/analyze?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","{myKey}");
},
type: "POST",
// Request body
data: "{'url':'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg'}",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
}
在我的 .htaccess 中我已经添加了:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
当我使用 hurl.it 测试请求时,它有效。就我的服务器而言,它没有。
看起来您正在为您的服务器设置 CORS headers,这意味着有人可以向您的服务器发出 cross-domain 请求。
Microsoft Cognitive Services 必须在他们的服务器上添加这些 headers 以便您可以向他们发出 cross-domain 请求,或者您必须使用 JSONP.