到目前为止,我无法从JavaScript查询我的Azure表。我按照https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx中的步骤描述了如何验证请求,但到目前为止我只得到一个Ajax错误回调。有谁知道这里出了什么问题?以下是我使用的代码:
CORS代码(C#控制台应用程序):
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
private const String ACCOUNT_NAME = "<ACCOUNT_NAME>";
private const String ACCOUNT_KEY = "<ACCOUNT_KEY>";
public Form1() {
InitializeComponent();
}
private CloudTableClient makeTableClient() {
String connectionString = "AccountName=" + ACCOUNT_NAME + ";AccountKey=" + ACCOUNT_KEY + ";DefaultEndpointsProtocol=https";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
return tableClient;
}
private void btnAdd_Click(object sender, EventArgs e) {
addRule(makeTableClient());
}
private void btnRemove_Click(object sender, EventArgs e) {
removeRule(makeTableClient());
}
public void addRule(CloudTableClient tableClient) {
try {
CorsRule corsRule = new CorsRule() {
AllowedHeaders = new List<string> { "*" },
AllowedMethods = CorsHttpMethods.Connect | CorsHttpMethods.Delete | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Merge
| CorsHttpMethods.Options | CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Trace,
//Since we'll only be calling Query Tables, let's just allow GET verb
AllowedOrigins = new List<string> { "*" }, //This is the URL of our application.
ExposedHeaders = new List<string> { "*" },
MaxAgeInSeconds = 1 * 60 * 60, //Let the browser cache it for an hour
};
ServiceProperties serviceProperties = tableClient.GetServiceProperties();
CorsProperties corsSettings = serviceProperties.Cors;
corsSettings.CorsRules.Add(corsRule);
tableClient.SetServiceProperties(serviceProperties);
} catch (Exception e) {
txtOutput.Text = e.ToString();
}
}
public void removeRule(CloudTableClient tableClient) {
try {
ServiceProperties serviceProperties = tableClient.GetServiceProperties();
CorsProperties corsSettings = serviceProperties.Cors;
corsSettings.CorsRules.RemoveAt(0);
tableClient.SetServiceProperties(serviceProperties);
} catch (Exception e) {
txtOutput.Text = e.ToString();
}
}
}
}
HTML / JavaScript代码:
function CallTableStorage() {
var accountName = "<ACCOUNT_NAME>";
var tableName = "<TABLE_NAME>";
var userId = "<PARTITION_AND_ROW_KEY>";
var secretKey = "<ACCOUNT_KEY>";
var partitionKey = userId;
var rowKey = userId;
var queryString = encodeURIComponent(tableName + "(PartitionKey='" + partitionKey + "',RowKey='" + rowKey + "')");
var urlPath = "https://" + accountName + ".table.core.windows.net/" + queryString + "?$select=adid";
var VERB = "GET";
var contentMD5 = "";
var contentType = "text/plain; charset=UTF-8";
var date = (new Date()).toUTCString();
var canonicalizedResource = "/" + accountName + "/" + queryString;
stringToSign = VERB + "\n" +
contentMD5 + "\n" +
contentType + "\n" +
date + "\n" +
canonicalizedResource;
var signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(CryptoJS.enc.Utf8.parse(stringToSign), CryptoJS.enc.Base64.parse(secretKey)));
$.ajax({
url: urlPath,
type: 'GET',
success: function(data) {
console.log('Success:', data);
},
beforeSend: function (xhr) {
xhr.setRequestHeader('x-ms-version', '2014-02-14');
xhr.setRequestHeader('x-ms-date', date);
xhr.setRequestHeader('Authorization', "SharedKey " + accountName + ":" + signature);
xhr.setRequestHeader('Accept', 'application/json;odata=nometadata');
xhr.setRequestHeader('Accept-Charset', 'UTF-8');
xhr.setRequestHeader('DataServiceVersion', '3.0;NetFx');
xhr.setRequestHeader('MaxDataServiceVersion', '3.0;NetFx');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
},
error: function(data) {
console.log('Error:', data);
}
});
}
window.onload = CallTableStorage;
答案 0 :(得分:0)
您可以尝试从stringToSign中删除内容类型,看看是否有效吗?或者,正如Michael Roberson建议的那样,为Content-Type添加一个setRequestHeader,看看是否有效。
此外,如果此javascript / html适用于客户端应用,则应谨慎,因为您的帐户密钥将以纯文本形式发送。