最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - It's possible to store json on amazon s3? - Stack Overflow

programmeradmin5浏览0评论

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....

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

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....

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

Share Improve this question asked Jun 13, 2013 at 12:00 TropicalistaTropicalista 3,13712 gold badges45 silver badges72 bronze badges 7
  • 1 While using content-type headers is certainly good, they are not required. If you know that a certain file contains JSON, you can just parse the response text with JSON.parse. In the end, a file contains either text or binary data anyway. How to process the data is a decision the client has to make. – Felix Kling Commented Jun 13, 2013 at 12:06
  • Unfortunately after some tests it seems that the json is not well formatted if I retrieve as text/plain so I cannot parse it... – Tropicalista Commented Jun 13, 2013 at 12:30
  • That does not have to do anything with content type. JSON is text. Maybe your JSON is invalid to begin with? – Felix Kling Commented Jun 13, 2013 at 12:34
  • Probably you are right. But when I create the json, I parse and it is ok. Then I store it on s3, but when I retrieve I cannot parse it – Tropicalista Commented Jun 13, 2013 at 12:46
  • Strange... are you sure you are actually getting any response? It could a same-origin-policy issue. – Felix Kling Commented Jun 13, 2013 at 12:47
 |  Show 2 more comments

3 Answers 3

Reset to default 7

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

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

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

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.

$.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.

发布评论

评论列表(0)

  1. 暂无评论