I have an AJAX function to send an argument and retrieve some json object from a python script. I try to send the value from an input text to the script
AJAX code
function ajax_get_json(){
var results = document.getElementById("results");
var hr = new XMLHttpRequest();
var tipo = document.getElementById('tipo').value;
var atributo = " " +tipo;
document.getElementById('texto').innerHTML = atributo;
hr.open("GET", "prov1.py", + atributo, true);
hr.responseType = "JSON";
hr.setRequestHeader("Content-Type", "application/json", true);
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var data = JSON.parse(hr.responseText);
results.innerHTML = "";
for(var obj in data){
results.innerHTML +="<tr><td>"+ data[obj].id+"</td><td>"+data[obj].nombre+"</td><td>"+data[obj].tipo+"</td></tr>";
}
}
}
hr.send(null);
results.innerHTML = "requesting...";
}
my python script is this
#!/usr/local/bin/python2.7
import sys
import cx_Oracle
import json
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
tipo = form.getvalue('tipo')
#print "Content-Type: text/html; charset=utf-8\n\n";
print "Status: 200 OK"
print "Content-type: application/json\n";
#print
lst_proveedores=[]
conn_str = 'user/pass@database'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute(""" select id_proveedor, nombre, tipo from mpc_proveedores where tipo = '%s' """ %tipo)
for row in c:
record1 = {"id":row[0], "nombre":row[1], "tipo":row[2]}
lst_proveedores.append(record1)
json_string = json.dumps(lst_proveedores)
print json_string
conn.close()
In command line the script work's fine (python prov1.py tipo=MMS) and retrieve from the database the data, but when I try to retrieve the data from AJAX the script send me an empty json object. using firebug I check the response and only appear [].
I think the AJAX function have some error, because appear send the argument empty (python prov1 tipo=). I am new with AJAX and no sure if I am not using properly the functions in AJAX or is in the python script. If anybody knows a better way to retrieve the data, let me know please
Help please!!!!