0

It's my first time using AJAX and I don't understand why I need to specify url parameter in a JS Ajax call.

{% block javascript %}
  <script>
    $("#id_username").change(function () {
      $.ajax({
        url: '/some_new_url/',
        data: {
          'something': ...
        },
        success: function (data) {
          if (data.is_taken) {
            alert("Data is already in DB");
          }
        }
      });

    });
  </script>
{% endblock %}

To my understanding, AJAX is used to do something on the server side without refreshing a page. So it shouldn't redirect to a new url upon sending a data to the server, and stay on the same url. And yet AJAX call requires url parameter.

And I don' really like this, because setting a new url means I have to add another url pattern in my app/urls.py.

re_path(r'^create/$', views.Some_View.as_view(), name='create'),

And as a consequence, make another view in my views.py

class Some_View(ListView):
    model = SomeModel
    fields = '__all__'

But, I already have a CBV that generates form fields on the user side and accepts user inputs. I only want to make my existing CBV to save data to DB using AJAX call.

Since I don't understand what the purpose of the url is, I don't know how to set up my new url pattern, and CBV. Can I get some explanation here?

++ This is just a bonus question, but my ultimate goal is to generate multiple form fields, and multiple Submit buttons that sends the respective form input data to the server using AJAX. If there's any advice on how to tweak AJAX code, I would appreciate it.

4
  • Ajax needs to send the data to the server. That requires a URL. How else could it work? Commented Jul 30, 2018 at 15:20
  • Send the data to the same url you have. Update that method to work for AJAX requests. Ref: django.http.HttpRequest.is_ajax Commented Jul 30, 2018 at 17:40
  • @SachinKukreja do you have link to any example code snippet that does that? including AJAX function, CBV, and urls.py? Commented Jul 30, 2018 at 21:31
  • I think if you search well on the internet, you will find a working example. Commented Jul 31, 2018 at 14:05

1 Answer 1

1

An AJAX request is just a regular HTTP request to a url on the server. The only difference between an AJAX request and a request made by an ordinary browser GET or POST is that with AJAX, the results that come back from the server are returned to your javascript function and then you get to decide what to do with those results.

So there's no automatic updating of anything.

If you want to save something on the server, you need a view there on the server which is capable of understanding the data you are sending in the AJAX request, saving it, and then sending back a response which, again, your javascript code needs to be able to understand.

But if you already have a view which is capable of doing what you want, you can use it for your AJAX request, you just have to send a request with everything in it that the view requires.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.