0

I was trying to get some information from API using ajax. I was able to post data but I was unable to get all the data from the server. I am new in the Django environment, though i have tried some references. I was wanted to get the data from the server using the API that I provide and show those data by using ajax get call.

References that I have followed:

1.Django Ajax Jquery Call

2.https://simpleisbetterthancomplex.com/tutorial/2016/11/15/how-to-implement-a-crud-using-ajax-and-json.html

3.https://www.sourcecodester.com/tutorials/python/11762/python-django-simple-crud-ajax.html

My code for get call:

         $.ajax({
            type: "GET",
            url: "/save_composition",
            dataType: "json",
            success: function(data) {
                alert(data)
            },
            error: function(xhr, textStatus) {
                   alert("error..");
            }});

Url section :

path('restore_composition/', views.restore_composition, name='restore_composition')

Views.py:

def restore_composition(request):
   data = SaveComposition.objects.all()
   return render(request, 'index.html', context={'data': data})
2
  • Where have you defined your save_composition method/url ? Commented Oct 18, 2019 at 11:51
  • I misunderstood at some point. The endpoint was the issue.Thanks for your co-operation. Commented Oct 18, 2019 at 13:15

1 Answer 1

1

This is how ajax calls works in Django framework.

def ajax_method():
    return HttpResponse(200)

url to the ajax call

path('save_composition', views.ajax_method, name='ajax_method')

You need to set the `url-path' without forward-slash

Ajax call

$.ajax({
        type: "GET",
        url: "save_composition",
        dataType: "json",
        success: function(data) {
            alert(data)
        },
        error: function(xhr, textStatus) {
               alert("error..");
        }});
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, it seems the right way and I already mentioned that I am new in Django and ajax environment, so thanks for your co-operation.

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.