# Django (AJAX post request)

**URL:** <https://forum.djangoproject.com/t/django-ajax-post-request/8023>\
**Category:** Using Django\
**Created:** [May 22, 2021, 8:49pm UTC](https://forum.djangoproject.com/t/django-ajax-post-request/8023 "2021-05-22T20:49:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andresherrera9](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andresherrera9/32/4546_2.png) [@andresherrera9](https://forum.djangoproject.com/u/andresherrera9)\
**Post date:** [May 22, 2021, 8:49pm UTC](https://forum.djangoproject.com/t/django-ajax-post-request/8023/1 "2021-05-22T20:49:16Z")

</div>

Hey there!

I want the client to be able to crop images stored in the app server (I use Cropperjs to crop the image, AJAX to send the post with the cropped image). Once the client crop the image, the server receives it and is used for further processing. So far I have this:

# View

```auto
from .models import Image

def main_view(request):
    
    obj = Image.objects.get(pk=1)
    context = {'obj':obj}
    return render(request, 'main.html',context)

```

# template + Cropperjs logic

```auto
<img src="{{obj.file.url}}" id="image" width="400px" >
{% csrf_token %}
<input type="button" id="confirm-btn" value="Start cropping" />

<script>
(Section for cropping the image)

$.ajax('media/images/', {
            method: 'POST',
            enctype: 'multipart/form-data',
            data: formData,
            processData: false,
            contentType: false,
            success() {
              console.log('Upload success');
            },
            error() {
              console.log('Upload error');
            },
          });
        });

</script>

```

I am receiving the error : Forbidden (CSRF token missing or incorrect.): /media/images/ for the post.  
I have done this with a form and it works (when client uploads their image). However in this case they are editing images stored in the server. Someone to shed light on this matter?

Thanks!

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [May 22, 2021, 9:47pm UTC](https://forum.djangoproject.com/t/django-ajax-post-request/8023/2 "2021-05-22T21:47:12Z")

</div>

It seems you’re forgetting to send the csrf token with the AJAX request.  
You can grab it from the DOM with JS.

```auto
<script>
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
</script>

```

[https://docs.djangoproject.com/en/3.2/ref/csrf/#acquiring-the-token-if-csrf-use-sessions-or-csrf-cookie-httponly-is-true](https://docs.djangoproject.com/en/3.2/ref/csrf/#acquiring-the-token-if-csrf-use-sessions-or-csrf-cookie-httponly-is-true)

---

<div class="post-metadata">

**Author:** ![andresherrera9](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andresherrera9/32/4546_2.png) [@andresherrera9](https://forum.djangoproject.com/u/andresherrera9)\
**Post date:** [May 22, 2021, 11:29pm UTC](https://forum.djangoproject.com/t/django-ajax-post-request/8023/3 "2021-05-22T23:29:03Z")

</div>

Yes, now I’m receiving the post request.

Thank you!
