Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
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 Jun 13 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 Jun 13 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 Jun 13 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 Jun 13 at 12:46
Strange... are you sure you are actually getting any response? It could a same-origin-policy issue. – Felix Kling Jun 13 at 12:47
show 2 more comments
up vote 1 down vote accepted

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)
        }
    }
})
share|improve this answer
I cannot say why the second one works – Tropicalista Jun 13 at 13:53
I think the first one magically works if the content-type is set correctly. The second one is explicitly parsing as JSON (and will probably break if you fix the content type). – BraveNewCurrency Jun 15 at 18:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.