It's possible to store json on amazon s3?

前端 未结 3 1344
天涯浪人
天涯浪人 2021-02-19 07:33

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 shou

3条回答
  •  孤城傲影
    2021-02-19 07:57

    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.

提交回复
热议问题