javascript 可以在亚马逊 s3 上存储 json 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17086553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:02:24  来源:igfitidea点击:

It's possible to store json on amazon s3?

javascriptjsonamazon-s3

提问by Tropicalista

I would like to store json file to my amazon s3 and then retrieve it with ajax request. Unfortunately it seems s3 does not allow content-type application/json....

我想将 json 文件存储到我的亚马逊 s3,然后使用 ajax 请求检索它。不幸的是,似乎 s3 不允许内容类型的应用程序/json ....

I should save my file as text/plain and then add header with php?

我应该将我的文件保存为文本/纯文本,然后使用 php 添加标题?

采纳答案by Tropicalista

I have found the problem. I was parsing the json in the wrong way.

我找到了问题所在。我以错误的方式解析 json。

$.ajax({
    url:"https://s3.amazonaws.com/myBucket/myfile.json",
    type:"GET",
    success:function(data) {
            console.log(data.property)
    }
})

Instead this works:

相反,这有效:

$.ajax({
    url:"https://s3.amazonaws.com/myBucket/myfile.json",
    type:"GET",
    success:function(data) {
        var obj = jQuery.parseJSON(data);
        if(typeof obj =='object'){
            console.log(obj.property)
        }
    }
})

回答by Dilshad PT

Change Metadata 'Value' in Key:Value pair to 'Application/json' from file properties, in AWS S3 console.

在 AWS S3 控制台中,将 Key:Value 对中的元数据“Value”从文件属性更改为“Application/json”。

回答by Manoj Selvin

To avoid parsing json use dataType property of ajax, Here we are expecting response as json so
dataType: "json"will automatically parse the json for you and can be accessed directly without JSON.parse(), in Success function body.

为了避免解析 json 使用 ajax 的 dataType 属性,这里我们期望响应为 json,因此
dataType: "json"会自动为您解析 json,并且可以在 Success 函数体中直接访问而无需 JSON.parse()。

$.ajax({
        url:"https://s3.amazonaws.com/myBucket/myfile.json",
        type:"GET",
        dataType: "json",    // This line will automatically parse the response as json
        success:function(data) {
            var obj = data;
            if(typeof obj =='object'){
                console.log(obj.property)
            }
        }
    })

dataType- is you telling jQuery what kind of response to expect. Expecting JSON, or XML, or HTML, etc. In our case it was JSON.

dataType- 你告诉 jQuery 期望什么样的响应。期待 JSON、XML 或 HTML 等。在我们的例子中它是 JSON。