0

How to call python variables in ajax success call?

I fed the HTML form inputs through ajax post call to python backend, and as part of ajax succcess call i need to call python variables to build the <tbody> of the html table.

{{ login_id }} doesn't seem to be working in Ajax, but does in html code. Some direction on this is greatly appreciated

this is a snippet from the ajax success code,

html += "<\td>" + "{{ login_id }}" + "<\/td>\", 

this results in undefined value.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Tom
  • 23
  • 6
  • Maybe you can return Json from your python script .... http://stackoverflow.com/questions/983855/python-json-encoding – Hackerman Dec 22 '15 at 11:51
  • i think it's already returning Json list, it's in this format: [["A", "B", "C"], ["D", "E", "F"], ...... – Tom Dec 22 '15 at 12:20
  • The line of code in your text, is that JavaScript? If so, it has two errors. – Mr Lister Dec 28 '15 at 13:01

1 Answers1

0

The {{login_Id}} works in your HTML template because it's further interpreted by python during the page construction.

In your ajax call response you gotta use JavaScript itself in order to update the current HTML.

The login_id value needs to be in the ajax response, then you'll do something like:

response = function (data) { document.getElementById("elementId").innerHtml = data.login_id; ... }

I hope that might help.

Thiago Melo
  • 1,157
  • 1
  • 14
  • 31
  • What elementId will this be, my table html is like below,
    AA BB CC
    , And in the python i'm returning only response, do i also return the python variables that i wanting to use, something like: return (response, login_id=login_id) ?
    – Tom Dec 22 '15 at 12:20
  • Hi Tom, are you using Django? If so, check this thread http://stackoverflow.com/questions/2428092/creating-a-json-response-using-django-and-python there you can see how to return a Json object in the python response. Then you can get this json in the ajax response function. – Thiago Melo Dec 22 '15 at 18:31
  • I'm using flask, is there a similar link for flask? i couldn't find it here and i still find it hard to get this done – Tom Dec 28 '15 at 10:48
  • Well, there is an example of how to return a JSON in Flask here: http://code.runnable.com/Up7oLHOstsZFAAAc/json-response-in-flask-for-python wishes it helps you. – Thiago Melo Dec 28 '15 at 21:44