There's more...

Another way to approach the same problem is to use Axios for sending the AJAX request. If you need to brush up on what Axios is, take a look at the Sending basic AJAX requests with Axios recipe.

The code for the submit method will become as follows (remember to add Axios as a dependency):

submit () {   axios.post('http://jsonplaceholder.typicode.com/posts', {     title: this.title,     body: this.body,     userId: this.userId   }).then(response => {     this.response = JSON.stringify(response,null,'  ')   }).catch(error => {     this.response = 'Error: ' + error.response.status   }) }

This code is perfectly equivalent, but it's much more expressive and concise than using native browser objects.

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.