【发布时间】:2016-07-24 13:26:26
【问题描述】:
我对@987654329@ 完全陌生,并且在为角度客户端设置CORS 时遇到问题。我正在使用这个module。我在nodejs 服务器上成功生成了sasToken。
我还检查了这些参考资料:Set Blob Service Properties、Cross-Origin Resource Sharing (CORS) Support for the Azure Storage Services 和 Windows Azure Storage: Introducing CORS,但我真的觉得在哪里做/放置这些代码令人困惑。
到目前为止,我所做的是:
在 AGULAR 控制器中:
// this is the service that generate the sasToken from the server
getService.getSasToken(filename)
.then(function (response) {
// response = {
// sasToken : "asa7sya....",
// token: "the shared key",
// account: "the storage account name"
// }
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type","application/xml");
xhr.setRequestHeader("x-ms-version", "2013-08-15");
xhr.setRequestHeader("Authorization", response.token);
xhr.setRequestHeader("myaccount", response.account);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type","application/xml");
xhr.setRequestHeader("x-ms-version", "2013-08-15");
xhr.setRequestHeader("Authorization", response.token);
xhr.setRequestHeader("myaccount", response.account);
} else {
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('PUT', 'https://foo.blob.core.windows.net?restype=service&comp=properties');
if (!xhr) {
throw new Error('CORS not supported');
}else{
// Response handlers.
xhr.onload = function() {
alert('Response from CORS request to ' + xhr.responseText);
azureBlob.upload({
baseUrl: "https://foo.blob.core.windows.net/bar/"+filename,
sasToken: "?"+response.sasToken,
file: blobFile,
progress: function (progress) {
console.log("progress", progress);
},
complete: function (complete) {
console.log("complete", complete);
},
error: function (error) {
console.log("error", error);
},
// blockSize: // What do I put here?
})
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
$.ajax({
type: "GET",
url: "../scripts/cors.xml", // local xml file
dataType: "xml",
success: function(xml){
console.log("xml", xml);
xhr.send(xml);
}
});
}
},function (error) {
console.log(error);
})
CORS.XML
<?xml version="1.0" encoding="utf-8"?>
<StorageServiceProperties>
<Logging>
<Version>1.0</Version>
<Delete>true</Delete>
<Read>false</Read>
<Write>true</Write>
<RetentionPolicy>
<Enabled>true</Enabled>
<Days>7</Days>
</RetentionPolicy>
</Logging>
<HourMetrics>
<Version>1.0</Version>
<Enabled>true</Enabled>
<IncludeAPIs>false</IncludeAPIs>
<RetentionPolicy>
<Enabled>true</Enabled>
<Days>7</Days>
</RetentionPolicy>
</HourMetrics>
<MinuteMetrics>
<Version>1.0</Version>
<Enabled>true</Enabled>
<IncludeAPIs>true</IncludeAPIs>
<RetentionPolicy>
<Enabled>true</Enabled>
<Days>7</Days>
</RetentionPolicy>
</MinuteMetrics>
<Cors>
<CorsRule>
<AllowedOrigins>*</AllowedOrigins>
<AllowedMethods>GET,PUT,POST</AllowedMethods>
<MaxAgeInSeconds>500</MaxAgeInSeconds>
<ExposedHeaders>x-ms-meta-data*,x-ms-meta-customheader</ExposedHeaders>
<AllowedHeaders>x-ms-meta-target*,x-ms-meta-customheader</AllowedHeaders>
</CorsRule>
</Cors>
<DefaultServiceVersion>2013-08-15</DefaultServiceVersion>
</StorageServiceProperties>
上面的代码是基于这个GUIDE
但是我仍然收到此错误:
以前有人做过吗?请分享您的代码。
提前致谢。
【问题讨论】:
-
仅供参考,CORS 强制在所有跨源请求之前发出飞行前 HTTP OPTIONS 请求,因此您还需要将 OPTIONS 设置为允许的方法
<AllowedMethods>GET,PUT,POST,OPTIONS</AllowedMethods>有MDN article about CORS 中有关飞行前 OPTIONS 请求的部分。 -
@ChristoferEliasson 我添加了 OPTIONS,还是一样。
标签: angularjs azure cors azure-blob-storage