Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have some issues retrieving info from python and try to show the data in a html page I get the date from a python script (data.py)

import cx_Oracle
import json

lst_proveedores=[{}]
conn_str = 'user/pass@database'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute('select id, name from provider')
for row in c:
    record1 = {"id":row[0], "name":row[1]}
    lst_proveedores.append(record1)
json_string = json.dumps(lst_proveedores)
print json_string
conn.close()

I try to parse the info with AJAX in a html page

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function ajax_get_json(){
    var results = document.getElementById("results");
    var hr = new XMLHttpRequest();
    hr.open("GET", "prov1.py", 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 += data[obj].id+" is "+data[obj].nombre+"<hr />";
                        }
            }
    }
    hr.send(null);
    results.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="results"></div>
<script type="text/javascript">ajax_get_json();</script>
</body>
</html>

but doesn't work I setup apache to execute python scripts and work with very simple scripts, but doesn't work when I retrieve data from the database How can I show the data in a html page? Or what language or framework may I can use to show the data Any advice I am desperate Thanks in advance

share|improve this question

First of all, you should try visit your python files in browser. If you can't see json print on page, there're problems in your server or python code.

If it works, that may be something wrong in your Ajax request.

You can use jQuery or zepto.js to help. They contain a method of Ajax: $.ajax.

You can visit: http://zeptojs.com And search "$.ajax" on the page for help; )

===============================================================

try this:

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

===============================================================

and this is my onreadystatechange function code, use it if it helps:

ajaxObject.onreadystatechange = function(){
    //console.info('[Ajax request process] url:' + url +'; readyState:' + ajaxObject.readyState + '; status:' + ajaxObject.status);
    if (ajaxObject.readyState == 4 && ((ajaxObject.status >= 200 && ajaxObject.status < 300) || ajaxObject.status == 304)){
        var result = null;
        switch (dataType){
            case 'text':
                result = ajaxObject.responseText;
                break;
            case 'xml':
                result = ajaxObject.responseXML;
                break;
            case 'json':
            default:
                result = ajaxObject.response ? JSON.parse(ajaxObject.response) : null;
                break;
        }
        if (typeof(success) == 'function'){
            success(result,url);
        }
    }else if (ajaxObject.readyState > 1 && !((ajaxObject.status >= 200 && ajaxObject.status < 300) || ajaxObject.status == 304)){
        console.warn('[Ajax request fail] url:' + url +'; readyState:' + ajaxObject.readyState + '; status:' + ajaxObject.status);
        if (typeof(error) === 'function' && errorCallbackCount == 0){error(url);errorCallbackCount++;}
        return false;
    }
}
share|improve this answer
    
Well I test the AJAX code and work with a simple json file, but with the python script don't work – Joseleg Jul 31 '14 at 2:18
    
@Joseleg what happen if you visit your python script directly in browser like chrome? – Maplemx Jul 31 '14 at 5:06

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.