Forums

Cannot call python program from XMLHttpRequest in javascript

EDIT: Solution found, down below.

I have written a number of web apps before using the wsgi interface and standard web servers on web hosts such as most typically apache. These apps generate web pages that allow the user to enter data, and then they call python programs using xmlhttprequest to get back data to then alter the web page dynamically.

I understand with this web server I am only allowed to have one python program (which means I would have to put many small python programs into one large one but that's okay). However, I cannot determine after many hours of trying how it is I refer to that program in an xmlhttprequest.

Any url that locates the python program results, when used in xmlhttprequest, in returning the source code of the program instead of executing it and returning its output.

It is as if the webserver is not configured to recognize that this is a program to run. But this is the one program that you allow to be run. I also tried to reference the program using the url of my site, since that program is run if one just types the url of the site into the browser. However, with this url it does not do anything at all.

Only when I give a url relative to the javascript program that is calling it, will it find the python program, and then, again, it just returns the source code.

I see someone else had this program but they were using flask, and they were told not to use the static system for serving up files. Well, I am using neither of those. I just want to use the simplest means, which is no web framework at all except what is provided by default.

EDIT: Okay, somehow I managed to solve it. I will record what I found out here in case anyone else ever has the same problem.

My program starts out from a static web page which is loaded, and then when data is entered there, it calls the python program via xmlhttprequest. It turns out that I had the correct url in the javascript. However, the problem was that I had not set up the opening page as a static page, using instead the url

 https://www.pythonanywhere.com/user/TrashEquilibrium/files/var/www/welcome.html

rather than something like for example

 https:/TrashEquilibrium.pythonanywhere.com/www/welcome.html

where www was set up appropriately, pointing to /var/www

I guess things are configured to not work with the first url so as to ensure that decent static names are set up that do not expose the actual location of the file...

ok, glad that you found that solution. This is perhaps relevant to other readers.

If you want to actually run / execute code, then you will have to use one of the web frameworks. (ie. there is no way to just execute a file on that path)