# Problem with ajax in Django

**URL:** <https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339>\
**Category:** Using Django\
**Created:** [March 2, 2020, 10:45am UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339 "2020-03-02T10:45:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![L1m0nchik](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@L1m0nchik](https://forum.djangoproject.com/u/L1m0nchik)\
**Post date:** [March 2, 2020, 10:45am UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/1 "2020-03-02T10:45:06Z")

</div>

Hello dear experts. I ask for your help and tips.  
js script:

```
save_walls.addEventListener("click", function () {
$.ajax({
url: '/save_events_json/',
            type: 'GET',
            contentType: 'application/json; charset=utf-8',        
            success: function(result) {
                alert(result);
            },
            error: function() {
                alert('Error!');
            }
        });
});

```

views.py:

```
def save_events_json(request):
    if request.is_ajax():
        message = "This is ajax"
    else:
        message = "Not ajax"
    return HttpResponse(message)

```

urls.py:

```
urlpatterns = [
    path('', views.index, name='index'),
    path('save_events_json/', views.save_events_json, name='save_events_json'),
]

```

What is the essence of the problem. After clicking on the save\_walls button, the alert “This is ajax” is displayed, but after closing the alert and going to the save\_events\_json page, it says “Not ajax”, what is the problem?

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 2, 2020, 11:27am UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/2 "2020-03-02T11:27:56Z")

</div>

Please format your examples nicely in future posts, so they are readable by people trying to help you.

As I understand what you’re seeing is the exact documented behaviour of `is_ajax`: [https://docs.djangoproject.com/en/3.0/ref/request-response/#django.http.HttpRequest.is\_ajax](https://docs.djangoproject.com/en/3.0/ref/request-response/#django.http.HttpRequest.is_ajax)

Note that is\_ajax will actually be deprecated in Django 3.1: [https://code.djangoproject.com/ticket/30997](https://code.djangoproject.com/ticket/30997) . You should consider making separate views for AJAX versus non-AJAX.

---

<div class="post-metadata">

**Author:** ![L1m0nchik](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@L1m0nchik](https://forum.djangoproject.com/u/L1m0nchik)\
**Post date:** [March 2, 2020, 7:50pm UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/4 "2020-03-02T19:50:32Z")

</div>

Thanks. Can you write how to more correctly pass an array or variable from JS to django. Can ajax be avoided at all?

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [March 2, 2020, 9:15pm UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/5 "2020-03-02T21:15:41Z")

</div>

I’m not sure what problem you’re trying to solve. What are you trying to do?

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [March 2, 2020, 9:57pm UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/6 "2020-03-02T21:57:17Z")

</div>

AJAX can be avoided if it’s only when the page loads: [https://adamj.eu/tech/2020/02/18/safely-including-data-for-javascript-in-a-django-template/](https://adamj.eu/tech/2020/02/18/safely-including-data-for-javascript-in-a-django-template/)

Otherwise for AJAX views you should look at `JsonResponse`: [https://docs.djangoproject.com/en/3.0/ref/request-response/#jsonresponse-objects](https://docs.djangoproject.com/en/3.0/ref/request-response/#jsonresponse-objects) . Or for more advanced usage, see Django REST Framework.

---

<div class="post-metadata">

**Author:** ![L1m0nchik](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@L1m0nchik](https://forum.djangoproject.com/u/L1m0nchik)\
**Post date:** [March 2, 2020, 10:36pm UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/7 "2020-03-02T22:36:23Z")

</div>

I am trying to transfer an array from JS to Django, for this I was thinking of using ajax. But maybe there are still ways, I don’t know, therefore I ask for advice.

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [March 3, 2020, 12:35am UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/8 "2020-03-03T00:35:57Z")

</div>

OK, well the code you have shown is the beginnings of that, except I’d maybe use POST instead of GET for saving the data in the JavaScript snippet. What is surprising you about the result you have described?

---

<div class="post-metadata">

**Author:** ![L1m0nchik](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@L1m0nchik](https://forum.djangoproject.com/u/L1m0nchik)\
**Post date:** [March 3, 2020, 9:30am UTC](https://forum.djangoproject.com/t/problem-with-ajax-in-django/1339/9 "2020-03-03T09:30:54Z")

</div>

I used Get because it only worked, which is strange. There is a page with a button, after it is pressed, alert appears with the message that need. That is, the function in django saw that ajax worked. But when loading a new page after clicking OK on alert, django the function returns that ajax did not work, how is it?
