Blog Post Index Example
The URL for your blogging backend that retrieves an index of all posts
looks like this:
http://wwwr.cs.unc.edu/emilyblog/posts.json
The responseJSON property of the Ajax.Response object that is sent to
the onSuccess handler will contain a JavaScript array. Each entry in
the array will be a object that represents each blog post. These entry
objects will each contain the following properties:
- id
- The numeric ID associated with this blog post.
- title
- The title of the blog post as a string.
- body
- The body of the blog post as a string.
- created_at
- The date that the blog post was created as a string.
Other notes:
-
When setting up the Ajax.Reqest object to fetch this URL, you need to
be sure to set the method property of the Ajax options object
to be 'get'. It usually defaults to 'post', but
the backend interprets HTTP post requests for this resource as a
request to create a new post. See the Creating A New Post Example.
-
The date property is a string, but it can be turned into a proper JavaScript
Date object by passing it to a Date constructor. You may want to do
this if you want to generate a different date format or otherwise
work with or manipulate the value that the date represents. See here for
documentation about the JavaScript Date objects.
To test this URL, click on the following button. Look at the code in
posts_index_example.js to see how it works.