Jquery Ajax Post Request Example With Images (Python/Django)

How to submit a post request along with images using the jquery ajax method.

Karan Kaul | カラン
5 min readJan 22, 2022

--

Using the $.ajax() method (no image)

It is quite straightforward to send a post request using jquery ajax to the backend if you don’t have any media file selected. See the function syntax below.

jquery ajax post method
jquery ajax post method

The function might look complex if you are new to this but it’s not. You don’t need to make yours this big, you can leave out things that you don’t need. Like in my case, the success function is actually setting a timeout so that I can display a message to the user and after 2 seconds, the user will then be redirected to the /profile page.

You can slim it down, depending on what you need to do. I will first explain the parts of this function below and then discuss about images at the end —

type : “POST”

This is simply the type of request we want to send. Other request types are GET, PUT, PATCH e.t.c

enctype : “multipart/form-data”

This is the type of encoding we would like to use. Different enc types are suitable for different scenarios. In the case of images or when our form contains any file…

--

--