Forums

jQuery and Web2py

I'm a noobie to pythonanywhere, so forgive what maybe is a noobie question...

I have a web2py app that uses a jQuery/ajax combination to do something. The web2py view has a link on it that uses the web2py ajax ":eval" capability. The method called by the ajax function builds a jQuery and returns it. The view should then evaluate the jQuery and execute it. The jQuery is supposed to populate a DIV on the view, using the jQuery html function.

This all works perfectly when i run the app on my local development machine. When I run it on pythonanywhere, the jQuery is built correctly, but rather than evaluating it, the jQuery string simply gets written to a new screen; all that appears is the jQuery.

Is there some quirk of jQuery on pythonanywhere that I'm missing? Any help will be greatly appreciated.

I'm no expert on Ajax/jQuery, but what do you mean by "The method called by the ajax function builds a jQuery and returns it.".

I thought mostly one returns plain text, JSON, HTML or XML from the Ajax controller function, and then use jQuery to insert it in a DIV - is that what you're doing?

I guess you might be seeing a differnce between versions, e.g. of Web2py or a library you're using.

Jim

Another possibility is that the content-type is incorrect. Compare the network traffic in your browser developer tools and see how the request and response differ between your development machine and PythonAnywhere. There are probably some clues there.

Some more detail: The web2py ajax function calls a method. The method builds and returns a string that contains a valid jQuery function:

tbl = "jQuery('#options_list').html('Example jQuery String');"
return tbl

This should inject the string "Example jQuery String" into the view division with an id of "options_list" and does so on my local development machine. Running on pythonanywhere, it returns a blank screen with only "jQuery('#options_list').html('Example jQuery String');" on it.

I have not looked at content-type yet; will do that right away and report back what I find.

This is not a pythonanywhere problem. In trying to track the problem down, I realized my local development machine was running an older web2py. To make everything consistent, I upgraded web2py to latest and now get the same behavior on my development machine.

Thanks for the responses to my question. I'll go to the web2py forums and see what I can turn up.

Cool. Let us know what you find.

This turned out to be one problem which, when solved, turned up another.

Problem one was that when updating to a newer version of web2py, the dev has to also update the web2py.js file that resides in the application's /static directory. That makes sense, of course; I just hadn't thought of it.

Once that was fixed, the Ajax callback in the view didn't work at all when the target = ':eval' although it had worked before. Turns out that ':eval' in the Ajax callback was an "undocumented feature" and somewhere between the web2py version I was using before this morning and the current version, to which I updated, the callback code was changed and ':eval' doesn't work anymore.

The answer is to use response.js. I made the switch and all seems to work again, at least on the dev machine. I'll update the pythonanywhere version later today (after I work out another small bug); I assume (gulp!) that all will work there, too.

Many thanks to Anthony, the guru of web2py, who told me about the upgrade problem and then was able to look into the callback code to see why ':eval' no longer worked.

Many thanks for posting that -- it's interesting for us and I'm sure it'll be useful for anyone googling for similar symptoms :-)