Page 1 of 1

MAUAHAHAH! My ajax test!!

Posted: Thu Jun 16, 2011 1:26 pm
by Isaac
http://testing.isaacg.net/ajax_test/ajaxtest.html
I'm going to be able to do tons with this, now that I understand it!!!

Re: MAUAHAHAH! My ajax test!!

Posted: Thu Jun 16, 2011 3:12 pm
by Jeff250
Yup, it's nifty. You can use $.post() wrapper instead of $.ajax() to make it even simpler.

edit: Ack, you're also closing your <div> tag with a </button> tag. :P

Re: MAUAHAHAH! My ajax test!!

Posted: Thu Jun 16, 2011 3:39 pm
by Isaac
Hahaha. Fixed. Thanks!

here's the python source, for the record:

Version 1 of code. Simple mode. This is all you need to get the example above:

Code: Select all

#!/usr/bin/python
import cgi,cgitb
form = cgi.FieldStorage()
mystr=form.getvalue("try")
print  """ Content-Type: text/html\n\n
<b>This is Python responding! Hello!!!</b> Here's what you sent:
<i>"%s"</i><br/><hr></hr>
""" % mystr

Here's the python code I'm running, with an if statement that responds differently if you try to access the cgi file directly.

Code: Select all

#!/usr/bin/python
import cgi,cgitb
form = cgi.FieldStorage()
mystr=form.getvalue("try")
print "Content-Type: text/html\n\n"
display ="pppppttt!!!<i> cheater</i>"
if mystr<>None:
	display = """<b>This is Python responding! Hello!!!</b> Here's what you sent:
<i>"%s"</i>
<br/>
<hr></hr>
""" % mystr
print display
HTML:

Code: Select all

<html>
<head><title>test</title></head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
<script>
$(document).ready(function(){
	$("#go").click(function(){
		$.ajax ({type:"post",data:"try=This text is from jQuery",url:"ajaxtest.cgi",
			success:function(result){ $("#mydiv").append(result);}
			});
		});

});

</script> 
<body>

<button id="go">Send string "This text is from jQuery"</button>
<div id="mydiv"></div>

</body>
</html>

Re: MAUAHAHAH! My ajax test!!

Posted: Thu Jun 16, 2011 4:11 pm
by Foil
I haven't done web development in years, but I remember AJAX seemed like a pretty efficient way to do things on-demand for users.

Re: MAUAHAHAH! My ajax test!!

Posted: Thu Jun 16, 2011 6:20 pm
by Jeff250
It's also time to start declaring a doctype and making sure your code validates at validator.w3.org. It looks like you're using HTML5-isms, so the HTML5 doctype would be a good choice. You don't want to spend an hour debugging your Javascript just to find out it's not working because the browser isn't parsing your HTML correctly. Older browsers are the most inconsistent about parsing invalid HTML--invalid HTML might work fine on one browser, say the one you use by default, but when you go and test it on another browser, it might mysteriously barf. Another reason to declare a doctype is that older versions of IE are even worse than ordinary at rendering HTML/CSS unless you declare a doctype.

Re: MAUAHAHAH! My ajax test!!

Posted: Thu Jun 16, 2011 8:47 pm
by Isaac
I will do this and read more on it too. This concept is new to me.


Also, should I allow a single webpage to call many different python scripts, streaming data back and forth with ajax, or should I have one jQuery ajax function talking to a single cgi file, so there's only one stream per user? I can imagine having different cgi files doing different jobs, all pushing data to the browser. That would make adding features easier, because each jQuery program could have its own Python script. On the other hand, I could have a master cgi file that deals specifically with all streaming data and imports all my py files.

Maybe it doesn't matter, because it might be streaming the same amount of data anyway.

Re: MAUAHAHAH! My ajax test!!

Posted: Fri Jun 17, 2011 1:06 am
by Jeff250
Whatever seems the most elegant. If one approach turns out uglier than you expected, you can always switch to the other more beautiful.

On a technical note, since you seemed to imply otherwise, if you go with the one master cgi script route, you can still have multiple jQuery functions (or multiple calls to the same function) querying it simultaneously. In other words, multiple scripts does not get you any additional parallelism.

Re: MAUAHAHAH! My ajax test!!

Posted: Fri Jun 17, 2011 7:51 am
by Isaac
Yeah, I think. On the other hand, even if I had a single jQuery function that relayed lists to a single cgi file, it would still behave as many jQuery functions, because each user would run a different instance of that function.

Another thing.
http://testing.isaacg.net/commentboxa/comment.html

I think this comment box is easier to work with if it's python script isn't part of a massive collection of scripts.

Re: MAUAHAHAH! My ajax test!!

Posted: Fri Jun 17, 2011 10:57 am
by fliptw
There is always maintainability to consider

ponder looking into lxml, it has a html tree builder.

Re: MAUAHAHAH! My ajax test!!

Posted: Sat Jun 18, 2011 8:08 am
by Isaac
fliptw wrote:There is always maintainability to consider

ponder looking into lxml, it has a html tree builder.
Hey!! I looked it up and that's PYTHON!!!! sweeet! Yeah, I'll check it out.
for my notes:
http://lxml.de/intro.html