PDA

View Full Version : Ajax response



Liminality
30 Apr 2009, 10:06 AM
Given a Ajax request that calls a python script on the server-side, how do I send a response back to the web page? Should be simple enough, just not sure how to do this. Thanks.

mitch_feaster
30 Apr 2009, 10:14 AM
are you using a framework (i.e. django or something) or is it just a cgi script?

Liminality
30 Apr 2009, 10:26 AM
No, I am not using django. I would presumably just be using python's cgi module...

mitch_feaster
30 Apr 2009, 10:33 AM
Ooof, that's no fun /:)

Well think of it this way: you just need to have some page that writes something (like maybe a return status) and nothing else. I'm not sure exactly how you would do that with the python cgi module but that's the basic concept.

It might help to look at some of the examples at the very bottom of this (http://extjs.com/learn/Tutorial:Introduction_to_Ext_2.0) page to see how it's done with various other web frameworks.

Liminality
30 Apr 2009, 11:11 AM
Yeah, I understand the concept, I am just unsure of how I would accomplish this using solely Python. My God, though, is this really such an esoteric subject? Are there not countless other people doing the exact same thing because I cannot find a single example of this. There are a million and three examples showing how to initiate an ajax request, but very few showing how to respond. Granted, it seems that there are many ways that one can go about this, depending on choice of language/framework--but there has to be someone out there who has done this with Python. Any takers?

evant
30 Apr 2009, 7:33 PM
I'm not sure why this is confusing? Just echo the content, as though you were serving up a dynamic html page.

eyurdakul
1 May 2009, 12:41 AM
Ext.Ajax.request({
url: 'your phyton script',
params: {
param1: 'value',
param2: 'value'
},
success: function(response, options){
var d = Ext.util.JSON.decode(action.response.responseText);
Ext.Msg.alert('SUCCESS', d.success_message);
},
failure: function(response, options){
var d = Ext.util.JSON.decode(action.response.responseText);
Ext.Msg.alert('ERROR', d.error_message);
},
scope: this
});

Ajax request has two functions, success and failure. Every json response must contain a success value i.e. {'success':true, 'success_message':'item is deleted'}. If the success parameter is true, ExtJS runs success function and vice versa. That's all i know about the ajax request.

Animal
1 May 2009, 12:54 AM
The requirement to send back a JSON object containing a success status is only for form submissions.

Ext.Ajax requests may return any data.

And the success/failure is communication success or failure. It does not impose any application requirements.

eyurdakul
1 May 2009, 4:31 AM
yes you are right animal. But if youu define success function in your ajax request, the success parameter indeed is not depended on the response. If the ajax request can succesfully send the parameters to the right url, success function will run. am i wrong?

Animal
1 May 2009, 5:30 AM
That is correct. If the server responds with any success HTTP status (200 to 299 (plus 1223 if you're on IE)) before the timeout (default 30 seconds) expires, then the success function is called.

Liminality
1 May 2009, 6:25 AM
Again, the actual request itself is not confusing... in the least. The thing that is perplexing is, just as evant stated, this python script consists of a single, simple line, which reads:

print "Content-type: text/html\n\n"

That is all the script is doing. But for whatever reason, I'm getting a 500 internal server error. This is the confusing part, because it shouldn't be happening.

Animal
1 May 2009, 7:35 AM
It sounds like you should post this to a Python forum.

Liminality
1 May 2009, 7:57 AM
Yeah. That would make sense, you know, because I'm using Python for less than one percent of this... and extjs for the other ninety-nine. I know Python quite well. I am just not sure how to generate the header for the Ajax response. Is it so illogical to assume someone here has done this before and can actually provide me with a useful response?

Animal
1 May 2009, 8:03 AM
If nobody knows Python (which from the lack of response appears top be the case), then yes!

eyurdakul
2 May 2009, 2:55 AM
well you don't need to know too much Phyton for such a simple script. I guess, the path to the phyton script is not correct or phyton is not running on the server.

Regarding your problems about sending headers, there should be some built in functions to send needed headers, please check http://docs.python.org/library/urllib2.html#urllib2.Request.add_header.


Request.add_header('name of the header', 'the value');