1

I am creating a website with Python, Javascript (JQuery) and AJAX. I know to execute a Python script with Ajax, but I don't know to return data to Javascript with Python.

For example, if there is a mistake in a form, I would like send an alert ("The mail is incorrect"). I know that with PHP, I can used echo "something" but I don't know how to take that with Python.

def secure_mail(mail):
    if mail == "":
        error = "the mail is empty"
        print error

Thank you for your help !

6
  • 1
    what kind of framework do you use? Commented Apr 3, 2015 at 12:07
  • PHP is much more "entangled" with web as python is. In python you have a webapp that answers on http requests. If you want to return an error with AJAX you just need to return something in your AJAX response that your clientside (javascript) will detect as an error. For displaying anything you need to handle that clientwise (javascript). Commented Apr 3, 2015 at 12:10
  • I don't use any framework. But maybe I should to use a framework. In fact, I am learning Python and I want to create my project by myself. Commented Apr 3, 2015 at 12:11
  • Maybe have a look a django (djangoproject.com) which is arguably the most popular python web framework. You'll find a good beginner tutorial on their website. Commented Apr 3, 2015 at 12:14
  • While you could write an web project from scratch it is much easier to use a framework. Flask and Django are both good candidates, I personally find Flask a bit "less magic" (as in easier to understand) and Django "more complete" (as in lots of features you might or might not want to use). For beginning I would prefer Flask, because it's easier to understant what's happening. Commented Apr 3, 2015 at 12:16

1 Answer 1

1

In your ajax success message or error function defined in your ajax method you would want to receive back JSON data which will contain your specific error as determined by your Python backend.

so in your python backend create a dictionary.

import json
returnValue = {'error': 'you messed up something'}

## serialize to json string
return json.dumps(returnValue)

Than in your ajax method data.error would equal "you messed up something"

Depending on your web framework of choice (django, flask) the concept of handling the request and returning a dictionary as a json string would be similar.

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

2 Comments

I must learn Flask or Django before to continue. Thank you so much for your help
no problem, i recommend Django, flask is great but Django has a ton of documents and very large community. Plus it can handle huge projects without having to bolt on a ton of extra stuff as opposed to a micro-framework like Flask.

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.