Last active
February 23, 2018 20:25
-
-
Save primedime/e9984ca6a158d9ee8862f37598d78655 to your computer and use it in GitHub Desktop.
Sample code for XMLHttpRequest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("&", "&").replace("’", "'"), | |
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