Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a Python script that fetch data from an Oracle Database and create a json object

#!/usr/local/bin/python2.7
import sys
import argparse
import cx_Oracle
import json
import cgi
import cgitb
cgitb.enable()

lst_proveedores=[{}]
conn_str = 'user/password@database'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
qstr = """ select id_proveedor, nombre, tipo from mpc_proveedores where tipo = '%s' and id_proveedor <> 0 """ %sys.argv[1]
c.execute(qstr)
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()

and i try to show the json object into a html page with this function in my js file:

function ajax_get_json(){
    var results = document.getElementById("results");
    var hr = new XMLHttpRequest();
    var tipo = "SMS"
    hr.open("GET", "prov.py?tipo=" + tipo, 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.response);
                        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...";
}

And i receive the error : SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data. Firebug say hat the error is in this line of code

var data = JSON.parse(hr.response);

And I don't have any idea to solve this problem. Example of the json data:

[{}, {"nombre": "AMNET", "id": 1, "tipo": "SMS"}, {"nombre": "BIZNET- Celumanix",                                                                                        "id": 3, "tipo": "SMS"}]

Any advice?

PD: someone knows a better way to fetch the data from a python script into a html page. without using frameworks like Django or Flask. Business requeriments says that the application don't have to use frameworks only pure python or cgi scripts

Thanks in advance

share|improve this question
    
The empty dictionary in as the first element is not your issue. Line 1 column 1 would be the very first character in the json string. Have you tried validating your json here? –  tyleha Aug 7 '14 at 17:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.