gpt4 book ai didi

javascript - 尝试在 JavaScript 中从 Azure 表存储获取值时出错

转载 作者:行者123 更新时间:2023-11-28 07:17:39 26 4
gpt4 key购买 nike

到目前为止,我无法从 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;

最佳答案

您可以尝试从 stringToSign 中删除内容类型并看看是否有效吗?或者,正如 Michael Roberson 建议的那样,为 Content-Type 添加 setRequestHeader 并查看是否有效。

此外,如果此 javascript/html 用于客户端应用程序,您应该小心,因为您的帐户 key 将以纯文本形式发送。

关于javascript - 尝试在 JavaScript 中从 Azure 表存储获取值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30675918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com