简体   繁体   中英

How to fetch JSON data stored in DocumentDB JSON file to clientside javascript?

I have a website developed using only client side code(JavaScript, Jquery, Html, CSS, Bootstrap). I do not intend to use SQL for now hence I was thinking of using DocumentDB as data related to this website is purely JSON. Currently my website does work without any server side coding but I feel that is not secure way of coding as I believe the JSON data which is currently part of my Javascript file, would be exposed to all once I publish/host website. I have gone through the tutorial of how to create DocumentDB Azure account, how to create database which consists of JSON files. But I do not have definite idea about how to fetch the NOW server side JSON data using client side Javascript. Right Now all the client side coding part is in Jquery and JavaScript and I am manipulating the JSON data which is in the same .JS file. Kindly suggest.

I am not terribly familiar with DocumentDB on Azure, but it seems to support a rest api for crud operations ( link ) So easiest method to implement the minimum amount of effort would be to use ajax to GET the information necessary.

$.ajax({
    type : 'GET',
    url : 'https://{databaseaccount}.documents.azure.com/dbs/{db-id}',
    headers : {
         //your headers here
    },
    success : function(jsondata){
         //do something
    }
})

A thing to note though, it is likely that you will need to hard code the credentials on the front end, if you insist on using only front end technology (as far as DocumentDB is concerned, it seems you need to provide authorisation token in the header link ). If security is your concern, you might want to do a bit of back end work for querying databases.

On the bright side, if you already know JS, nodejs is a perfect place to start oO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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