Skip to content

Instantly share code, notes, and snippets.

@primedime
Last active February 23, 2018 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primedime/e9984ca6a158d9ee8862f37598d78655 to your computer and use it in GitHub Desktop.
Save primedime/e9984ca6a158d9ee8862f37598d78655 to your computer and use it in GitHub Desktop.
Sample code for XMLHttpRequest
function getAllCategories() {
const allCategories = [417, 418, 419, 420, 421, 422]
for (let categories of allCategories) {
let ourRequest = new XMLHttpRequest()
ourRequest.open('GET', 'https://www.orlandoattractions.com/wp-json/wp/v2/posts?categories=' + categories)
ourRequest.onload = () => {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
const data = JSON.parse(ourRequest.responseText)
//console.log(data)
for (let blogData of data) {
let blogPost = {
title : blogData.title.rendered.replace("&#038;", "&").replace("&#8217;", "'"),
blogURLString : blogData.link,
imageURLString : blogData.better_featured_image.source_url,
category : categories
}
saveBlogToParse(blogPost)
//console.log(blogPost)
// console.log('title: ' + blogPost.blogTitle)
// console.log('URL: ' + blogPost.blogURL)
// console.log('Featured Image: ' + blogPost.blogFeaturedIMG)
}
} else {
console.log("We connected to the server, but it returned an error.")
}
};
ourRequest.send()
}
}
function saveBlogToParse(blog) {
const Blog = Parse.Object.extend('Blog')
const parseBlog = new Blog()
parseBlog.save(blog, {
success: function(obj) {
console.log('Successfully saved ' + obj.id + ' ' + blog.title)
},
error: function(err) {
console.error(err)
}
});
}
getAllCategories()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment