使用 jQuery 从 Azure Blob 存储查询 JSON 数据

发布于 2024-10-28 01:43:37 字数 273 浏览 1 评论 0原文

我在 Azure Blob 存储中有一些数据。数据是 JSON,并且已使用“application/json”内容类型保存。

我的应用程序将托管在“myapp.com”,该域包含“myapp.cloudapp.net”的 CNAME。我想我应该创建一个指向我的 Azure 存储的自定义域名,例如“storage.myapp.com”。

但然后呢?我可以使用 JSONP 或其他方式对 Azure 存储进行 JSON ajax 调用吗?

如何更好地做到这一点?

多谢。

I have some data in an Azure blob storage. The data is JSON and it has been saved with the "application/json" content type.

My app would be hosted at "myapp.com", a domain that contains a CNAME to "myapp.cloudapp.net". I guess I should create a custom domain name like "storage.myapp.com" that poins to my Azure storage.

But then? Can I use JSONP or other way to make JSON ajax calls to Azure storage?

How would be the better way to do this?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2024-11-04 01:43:37

嗯,显然 Azure blob 存储不直接支持 JSONP,但这是可以做到的。

例如,如果我将此 JSON 存储在 Azure blob 中:

{"Name":"Valeriano","Surname":"Tortola"}

我尝试:

<script type="text/javascript">

    $.getJSON("https://myaccount.blob.core.windows.net/jsonptests/data?jsoncallback=?",
             function (data) {
                 alert(data.Name);
             });
</script>

它不起作用。好吧,实际上浏览器下载了数据,但没有回调。因此,考虑到 JSONP 的工作原理,如果我使用回调函数保存此 JSON:

dataCallback({"Name":"Valeriano","Surname":"Tortola"})

我这样做:

<script type="text/javascript">

    function dataCallback(data) {
        alert(data.Name);
    }
</script>

<script type="text/javascript" src="https://myaccount.blob.core.windows.net/jsonptests/data"></script>

然后执行 dataCallBack :) 缺点是回调函数名称必须进行编码,但它是总比没有好。

快乐的日子,但如果有人有更好的方法那就太好了。

干杯。

Well, apparently Azure blob storage doesn't support JSONP straightaway, but it can be done.

For example, if I store this JSON in an Azure blob:

{"Name":"Valeriano","Surname":"Tortola"}

And I try:

<script type="text/javascript">

    $.getJSON("https://myaccount.blob.core.windows.net/jsonptests/data?jsoncallback=?",
             function (data) {
                 alert(data.Name);
             });
</script>

It doesn't work. Well, actually the browser download the data but there is no call back. So, considering how JSONP works, if I save this JSON with the callback function:

dataCallback({"Name":"Valeriano","Surname":"Tortola"})

And I do:

<script type="text/javascript">

    function dataCallback(data) {
        alert(data.Name);
    }
</script>

<script type="text/javascript" src="https://myaccount.blob.core.windows.net/jsonptests/data"></script>

Then the dataCallBack get executed :) The disadvantage is that the callback function name has to be harcoded, but it's better than nothing.

Happy days, but if anyone has a better way would be great.

Cheers.

久随 2024-11-04 01:43:37

Windows Azure Blob Storage REST 接口返回 XML (POX),而不是 JSON...但是从 JavaScript 查询很简单!使用 restype=container 和 comp=list 调用容器 URL:

$(document).ready(function () {         
    // Retrieve list of Blobs
    var containerUrl = 'http://tcontepub.blob.core.windows.net/json/';
    $.ajax({
        type: 'GET',
        url: containerUrl + '?restype=container&comp=list',
        dataType: 'xml',
        success: listBlobs
    });
});

然后您可以对返回的 XML 进行基本解析。这里我将提取 URL 并将其显示在 div 中。

function listBlobs(xml) {
    $(xml).find('Blob').each(function() {
        var url = $(this).find('Url').text();
        $('#panel').append(url + '<br />');
    });
}

我已经在 HTML 页面中对此进行了测试,该页面本身存储为 Blob。

不幸的是,我担心 JavaScript 的“同源策略”会让它在实践中变得相当困难。

The Windows Azure Blob Storage REST interface returns XML (POX), not JSON... However it's simple to query from JavaScript! Call you container URL with restype=container and comp=list:

$(document).ready(function () {         
    // Retrieve list of Blobs
    var containerUrl = 'http://tcontepub.blob.core.windows.net/json/';
    $.ajax({
        type: 'GET',
        url: containerUrl + '?restype=container&comp=list',
        dataType: 'xml',
        success: listBlobs
    });
});

Then you can do a basic parsing of the XML returned. Here I will extract the URL and display it in a div.

function listBlobs(xml) {
    $(xml).find('Blob').each(function() {
        var url = $(this).find('Url').text();
        $('#panel').append(url + '<br />');
    });
}

I have tested this in an HTML page that was itself stored as a Blob.

Unfortunately, I'm afraid the JavaScript "Same Origin Policy" will make this fairly difficult to use in practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文