Vue.js·

POST Request in Nuxt 3

Post requests in Nuxt 3 aren’t as obvious as you might imaging. In this post I’ll show you everything you need to get up and running and avoid common mistakes!

POST Requests in Nuxt 3

In this post we are going to look at POST requests in Nuxt 3. This is something that should be really simple to do, and to be honest with you it is. However if you are a moron like me you might find yourself spending two days trying to figure it out. In this post I'll go over everything you need to get POST requests working in Nuxt 3. I'll over go over some of the issues I encountered when trying to figure out how to make it work, and the solutions. I'll even go over fixing CORS issues! You're welcome. Feel free to follow along with the video, or checkout the detailed instructions below.

Create a new Nuxt 3 Project (optional)

If you are already setup with an existing Nuxt 3 project you can skip this step. If you haven't already got a Nuxt 3 project set up, go ahead and do this. Instructions on how to do this can be found in the nuxt documentation but its just a case of running the following commands:

terminal
npx nuxi init

Once you have run this command, navigate into your newly created project, install it, and start the development server. Like this:

terminal
cd 
npm install
npm run dev -- -o

You now have your Nuxt 3 project setup and ready to go!

Create a form in Nuxt 3 to Submit a POST Request from

In our Nuxt 3 project we are going to create a form. This form is where we are going to submit our POST request from.

Our Form

Within our template tags, we can create our form. Create your form with whatever inputs you require. For our form we will have the inputs: 'name', 'email' and 'message'. We will bind these to a formData object using v-model. Checkout the code below and make sure you have something similar. Please note, I have removed some of the markup that you see in the video to make it easier to see what's going on, and also because I am using tailwindcss classes in the video which you don't need.

Comments

No comments yet.

Be the first to share your thoughts!

Join the discussion