Ajax + Python. , how to return response text to Javascript.

1,154 views
Skip to first unread message

prabhakar

unread,
Dec 10, 2009, 1:20:47 AM12/10/09
to Trac Users
Hi,
I want to know how can we return reponseText to Javascript from
Python file. I tried to send the data to python file by using GET
method, it is working fine but the thing is how to return the
resposeText (Result) to Javascript.
Here i mentioned my code below,


Test.html:
----------------------------------------------------------------------------------------------------------------------------------------
<form name="name1" method="GET">
Name: <input type="text" id="myname" name="name1" onblur="showHint
()" />
<span id="txtHint"></span>
</form>
----------------------------------------------------------------------------------------------------------------------------------------

Js File:
---------------------------

var xmlhttp;
function showHint()
{
var str=document.getElementById("myname").value;
if (str!=''){
xmlhttp=GetXmlHttpObject();
var url="http://localhost:5656/psltrac_internal/admin/general/
basics/";
url=url+"?q="+str;
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.send(null);
}
}

function stateChanged()
{
alert(xmlhttp.readyState)
if (xmlhttp.readyState==4)
{
alert(xmlhttp.responseText);
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
----------------------------------------------------------------------------------------------------------------------------------------

Python Files
-----------------------
if req.method == 'GET':
if str(req.args.get("q"))!='' and str(req.args.get("q"))!
='None':
print "hello"

Here in python file i tried to send the responseText as
print
return
and req.send_header() all the methods failed to return responseText.

Olemis Lang

unread,
Dec 10, 2009, 7:55:01 AM12/10/09
to trac-...@googlegroups.com
On Thu, Dec 10, 2009 at 1:20 AM, prabhakar <prabha...@gmail.com> wrote:
> Hi,

:o)

> Here i mentioned my code below,
>
> Js File:
> ---------------------------
>
> var xmlhttp;
> function showHint()
> {
> var str=document.getElementById("myname").value;
> if (str!=''){
>        xmlhttp=GetXmlHttpObject();
>        var url="http://localhost:5656/psltrac_internal/admin/general/
> basics/";

This URL (since it's under /admin) should require you to have
TRAC_ADMIN permission . Try to monitor the requests with Firebug and
probably you'll find some `Access denied` HTTP errors

>
> Python Files
> -----------------------
> if req.method == 'GET':
>            if str(req.args.get("q"))!='' and str(req.args.get("q"))!
> ='None':
>                 print "hello"
>
> Here in python file i tried to send the responseText as
> print
> return
> and req.send_header() all the methods failed to return responseText.
>

If you are using Trac request objects then you need to write a request
handler (i.e. implement IRequestHandler interface ;o). If you are
using print statement then you should run it as a CGI script outside
Trac address space

;o)

--
Regards,

Olemis.

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:
Depurando errores en aplicaciones web CGI con Python -
http://feedproxy.google.com/~r/simelo-es/~3/xxyDHHH1YZ8/depurando-errores-en-aplicaciones-web.html
Reply all
Reply to author
Forward
0 new messages