How it works...

Most of the magic happens in the submit method. In the first line, we are creating an XMLHttpRequest object, which is a native JavaScript mechanism to make AJAX requests:

const xhr = new XMLHttpRequest()

We then use the open and setRequestHeader methods to configure a new connection; we want to send a POST request, and we will send some JSON along with it:

xhr.open('post', 'http://jsonplaceholder.typicode.com/posts') xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')

Since we are interacting with a RESTful interface, the POST method means that we expect our request to modify data on the server (in particular, create a new post), and that issuing the same request more than one time will get different ...

Get Vue.js 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.