简体   繁体   English

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

[英]It's possible to store json on amazon s3?

I would like to store json file to my amazon s3 and then retrieve it with ajax request.我想将 json 文件存储到我的亚马逊 s3,然后使用 ajax 请求检索它。 Unfortunately it seems s3 does not allow content-type application/json....不幸的是,似乎 s3 不允许内容类型的应用程序/json ....

I should save my file as text/plain and then add header with php?我应该将我的文件保存为文本/纯文本,然后使用 php 添加标题?

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)
        }
    }
})

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

To avoid parsing json use dataType property of ajax, Here we are expecting response as json so为了避免解析 json 使用 ajax 的 dataType 属性,这里我们期望响应为 json 所以
dataType: "json" will automatically parse the json for you and can be accessed directly without JSON.parse(), in Success function body. dataType: "json"会自动为你解析 json 并且可以直接访问,无需 JSON.parse(),在 Success 函数体中。

$.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. dataType - 你告诉 jQuery 期望什么样的响应。 Expecting JSON, or XML, or HTML, etc. In our case it was JSON.期望 JSON、XML 或 HTML 等。在我们的例子中它是 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM