I have a javascript file where I am trying to set a variable using data sent from a python cgi script.
I am trying to use the ajax get method, and setting the variable within that method. Here is how I am doing it in the javascript.
$.get('cgi-bin/populate_usernames.py', function(data) {
users = data
});
console.log(users)
In the python code, I have the following:
#!/usr/bin/python
import os
data = ["Bob", "Jim"]
For some reason, the console log fails because users is undefined. I think it is because the content type is incorrect for data. Is there a way for me to send a JSON array from the python code so that users has the correct contents of Jim and Bob?
$.getis asynchronous. You will have to put theconsole.log(users)inside the{ }of the callback you pass to$.get.