0

Has anyone written code that allows Javascript to communicate with mod_wsgi using Ajax? I'm looking for a very simple example that I can build off of (e.g. uppercase a string and return it).

The examples that I've seen use PHP/ASP. When I substitute wsgi for 'php' or 'asp', the program fails with a syntax error.

I've spent 2 days searching for example code but so far I've been unsuccessful.

I don't know php/asp and I don't want to add another language to the system if at all possible.

I'd appreciate any help you can offer. Thank you.

2
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by JustAnotherJoe
0

JAJoe,

Javascript is completely dumb when it makes an ajax request by whatever means (d.i.y. or with jQuery, Prototype etc). It submits a URL in the same way as the browser would if you typed the same url into its address bar and clicked GO. There's no native client-side filtering or validation of URLs. In this respect, the AJAX process is "rubbish in rubbish out".

The next thing Javascript knows is a bunch of readystate changes, hopefully leading to a successful response (.readyState == 4 && .status == 200).

To find out what's going wrong, you can do one of two things (or maybe both):

  1. Inspect the httpRequest.readystate and .status changes, typically by writing them to a "debug" div on your web page (or to eg. jQuery's "console", in which I'm not an expert). Even with d.i.y code, the javascript for this is pretty trivial. There are plenty of resources on the web to tell you what the various readystate/status values mean.
  2. Look in your server logs to see how the request was handled. Your server may include tools to facilitate this (typically via cPanelX or similar).

For most people including me (I know at least one who would disagree), 1 is much simpler than 2, so I would start with finding out what readystate changes occur.

Airshow

Edited by Airshow: n/a

0

The loadXMLDoc() function returns a 'syntax error' message when it comes across the first import statement in the Python code.

I'm following the Javascript code line-by-line using Firebug.

When I click the button,the readyState == 1 and status == 0
The send function executes.
The readyState is now == 4 and the status == 0
The send function executes.
Then I get the syntax error. It appears that the Python code isn't being executed at all but simply read in.

From the apache log, I see the following which I don't get running other mod_wsgi modules. According to what I've found, the KeyError is supposed to be benign.

So I'm still wondering if the Ajax code is wrong. Suggestions?

[Sun Nov 28 12:31:31 2010] [error] Exception KeyError: KeyError(-1217095936,) in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
[Sun Nov 28 12:31:32 2010] [info] mod_wsgi (pid=18939): Cleanup interpreter ''.
[Sun Nov 28 12:31:32 2010] [info] mod_wsgi (pid=18939): Terminating Python.
[Sun Nov 28 12:31:32 2010] [error] Exception KeyError: KeyError(-1217095936,) in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
[Sun Nov 28 12:31:33 2010] [info] removed PID file /var/run/apache2.pid (pid=18935)
[Sun Nov 28 12:31:33 2010] [notice] caught SIGTERM, shutting down
[Sun Nov 28 12:31:33 2010] [info] mod_wsgi (pid=18935): Terminating Python.
[Sun Nov 28 12:31:33 2010] [info] mod_wsgi (pid=19073): Initializing Python.
[Sun Nov 28 12:31:33 2010] [notice] Apache/2.2.14 (Ubuntu) mod_wsgi/2.8 Python/2.6.5 configured -- resuming normal operations
[Sun Nov 28 12:31:33 2010] [info] Server built: Nov 18 2010 21:19:34
[Sun Nov 28 12:31:33 2010] [debug] worker.c(1757): AcceptMutex: sysvsem (default: sysvsem)
[Sun Nov 28 12:31:33 2010] [info] mod_wsgi (pid=19076): Attach interpreter ''.
[Sun Nov 28 12:31:33 2010] [info] mod_wsgi (pid=19077): Attach interpreter ''.
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","mytest.wsgi",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Sherlock&lname=Holmes");
}
</script>
</head>
<body>

<h2>AJAX</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">Request data</button>
 
</body>
</html>
0

readyState == 4 with status == 0 is normally only associated with "file browsing" ie. requesting files from the local file system, not via an HTTP server.

That would be consistent with your observation that the Python code isn't being executed.

The page requested via ajax will be relative to the URL of the originally served main page unless you pass an absolute URL (starting with http://).

Airshow

Edited by Airshow: n/a

0

All code is served up from /var/www. However, I changed the code to this:

xmlhttp.open("POST","http://127.0.0.1/var/www/gethint.wsgi",true);

Which now gives me this perplexing error:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: file:///var/www/ajaxtest.html :: showHint :: line 28" data: no]

I made certain all the files in the /var/www have group read/write/execute permissions set.

sudo chmod g+x /var/www
0

This works:

http://127.0.0.1/ajaxtest.html

Follow-up:

OMG! I'm so stupid. I already have 'http://127.0.0.1' in my other html code and for whatever reason I didn't add it this time. I can't believe I burned all this time for nothing. If you didn't point it out to me, I would have wasted god knows how many more hours.

Thank you.

0

another duplicate post (and I pressed the 'post' button only once)

Edited by JustAnotherJoe: duplicate post

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.