1

The following jQuery will call a Python script. The Python script is able to receive the post data. However, the jQuery is unable to receive the response by the Python script.

$(document).ready(function(){
  $("#dd").blur(function(){
    $.post("http://127.0.0.1:8000/validation/trial/",
    {
      dd:document.getElementById("dd").value
    },
    function(data){
      alert("Data: " + data);
    });
  });
});

The following is the python script:

def trial(request):
    dd = request.POST.get('dd')
    print dd
    return HttpResponse(dd)
6
  • You have to return your value to send it to the JQuery Commented Dec 23, 2013 at 16:50
  • I used a HttpResponse but it did not work too. Commented Dec 23, 2013 at 16:59
  • Have you tried just a straight return? Commented Dec 23, 2013 at 17:03
  • Do you get an error in the javascript console? If you set a Content-Type of json, you should also return json: import json; dd = json.dumps({'yourkey': dd}); return ... Commented Dec 23, 2013 at 17:03
  • I used both json and non-json. All did not work. Commented Dec 23, 2013 at 17:06

2 Answers 2

1

In Django, printing things does not send them back to the client; you need to actually return a response. See the Django tutorial part 3:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
Sign up to request clarification or add additional context in comments.

1 Comment

Please update your question with your new code (show your whole view please)
0

I have found the solution.

$(document).ready(function(){
  $("#dd").blur(function(){
    $.post("http://127.0.0.1:8000/validation/trial/",
    {
      dd:document.getElementById("dd").value
    },
    function(data){ // <-- note that the variable name is "data"
      alert("Data: " + data);
    });
  });
});

The returned variable in the Python script needs to have the same variable name.

def trial(request):
    data = request.POST.get('dd')
    return HttpResponse(data)

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.