Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 665205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:41:02+00:00 2026-05-13T23:41:02+00:00

I am trying to understand how Django + Jquery and Ajax calls work together.

  • 0

I am trying to understand how Django + Jquery and Ajax calls work together.

It is just a simple /test/ url that shows a single input form, once submitted retrieves the answer from the server via ajax.

For that I have written a very small view:

def test(request):
    if request.is_ajax():
        from django.http import HttpResponse
        post_text = request.POST.get("post_data")
        return HttpResponse("{'response_text': '"+post_text+" recieved.'}", mimetype="application/json")
    else:
        return render_to_response('test.html', {},context_instance =RequestContext(request))

And I have written this url rule to my urlpattern at the urls.py:

(r'^test/$', 'myapp.views.test'),

And here is my test.html template:

<html>
  <head><title>template</title></head>

  <script type="text/javascript" src="/media/js/jquery.js"></script>

  <script type="text/javascript">
    $(document).ready(function() {
        $('#post_form').submit(function(event){
            event.preventDefault(); // cancel the default action
            var form = this;
            var data = {}
            data.post_data = $(form).find('input[@name=our_text]').val();

            $.post("/test/", 
                data, 
                function(responseData) {
                  alert(responseData.response_text);
                },
                "json"
            );
        });
    });
  </script>

  <body>
    <form id="post_form" method="post">
      INPUT: <input type="text" name="our_text" />
      <input type="submit" value="Add" />
    </form>
  </body>
</html>

It doesn’t seem to do any reply on my http://www.mysite.com/test/ once I fill the input field and submit. What can be the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:41:02+00:00Added an answer on May 13, 2026 at 11:41 pm

    jQuery 1.4 won’t parse invalid JSON. As Alex Gaynor mentioned, your JSON is invalid, since it’s using single quotes, rather than double quotes.

    Writing JSON by hand is silly. Use a library to convert python datatypes to JSON. Again, as Alex mentioned, Django ships simplejson for you already. Alternatively, if you’re using Python2.6 or later, json is part of the standard library http://docs.python.org/library/json.html

    from django.http import HttpResponse
    from django.utils import simplejson
    
    def test(request):
        if request.is_ajax(): 
            post_text = request.POST.get("post_data")
            response_dict = {'response_text': '"+post_text+" recieved.'}
            return HttpResponse(simplejson.dumps(response_dict), mimetype="application/json")
        else:
            return render_to_response('test.html', {},context_instance =RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to understand how ajax works with django several times, but looks
I've been trying to understand what's the optimal way to do Ajax in Django
I understand that Django won't let you save a incomplete fields in a single
I am trying to understand how it's possible to use http://code.google.com/p/django-simple-captcha/ with django comments.
I'm trying to understand the django admin better and at the same time, I'm
I am learning Django and I am trying to understand the use of models.py
trying to understand how custom admin commands work, I have my project named mailing
In my quest to understand queries against Django models, I've been trying to get
I'm trying to atomically increment a simple counter in Django. My code looks like
I'm new to Django and trying to understand the preferred means of operation when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.