Cockpit-CMS: How to access data on a simple HTML page

1.7k views Asked by At

I have looked through the documentation and it's straight forward up until it time to access the data you have created in the CMS. I've created a collection entry for pages with only two entries

page-title

and

page-content

The doc says you have to generate the API key before trying to access the data. I've done that. But the next step isn't straight forward to a non-developer. It seems all the information for accessing the API is written in JSON.

I've tried using the advised example

<script>

fetch('/api/cockpit/authUser?token=0a014d090bf541dc7482939f22841e')
    .then(res => res.json())
    .then(res => console.log(res));

but I get an error in the console, so I tried

<script src="/api/cockpit/authUser?token=0a014d090bf541dc7482939f22841e'"></script>

and still get an error.

Can anyone explain, in not so egghead terms how to

  1. access the API and display the data on a simple HTML page
  2. explain what I may be doing wrong in the first place

Note: I'm not trying to tie Cockpit with some other convoluted tool to get this to work, I just want to use access the data from a local cockpit cms and display the entry data on a simple html page -- if that possible.

1

There are 1 answers

0
Daniel On

Sorry If It's a late response.

The inserted example is used for user authentication not for entries get.

If you want to retrieve the "page" collection you need to use the /api/collections/get/{collectionname} endpoint. So if the collection name is page the method would be

 var variable1 = fetch('https://websitename.com/api/collections/get/page?token=xxtokenxx')

change xxtokenxx with your token.

This will provide an array of items which you can then place through javascript in the page. Use the first .then() to do stuff on success request, and the second for the logic on failed request.