I am trying to make a webpage with Ajax.
Example:
I create a Perl/CGU file that triggers a simple post;
File: ..test.cgi?name=Thomas
Text back: Your name is Thomas!
I create a html file that can use the post, but then the page have to reload. I use text input and a button.
How can I use Ajax, Perl and JSON easy together? This is how it should work together, but how?
Html + Ajax/JavaScript CALL Perl + "JSON-perl-string" RETURN-TO Ajax CONVERT-JSON -> Html
I am trying to make a webpage with Ajax.
Example:
I create a Perl/CGU file that triggers a simple post;
File: ..test.cgi?name=Thomas
Text back: Your name is Thomas!
I create a html file that can use the post, but then the page have to reload. I use text input and a button.
How can I use Ajax, Perl and JSON easy together? This is how it should work together, but how?
Html + Ajax/JavaScript CALL Perl + "JSON-perl-string" RETURN-TO Ajax CONVERT-JSON -> Html
Share Improve this question edited May 7, 2009 at 5:12 brian d foy 133k31 gold badges212 silver badges604 bronze badges asked May 5, 2009 at 11:05 jeje1983jeje1983 1151 gold badge3 silver badges7 bronze badges5 Answers
Reset to default 12For JSON try the CPAN JSON module.
For using the XMLHttpRequest I recommend these wonderful tutorials from IBM.
Mastering Ajax, Part 1: Introduction to Ajax
The two articles you'll probably be most interested in are these two:
Mastering Ajax, Part 10: Using JSON for data transfer
Mastering Ajax, Part 11: JSON on the server side
You can get the entire 11 Part series using this search link.
You just need to have your application return JSON (you can just use the JSON module on CPAN for this) instead of HTML. This means you need a Content-type header of application/json instead of text/html and then you need to use that JSON in your Javascript (using a Javascript library like jQuery or Prototype is your best bet here).
jquery provides very easy ajax and JSON support with their API, It handles all of the XMLHttpRequest objects for you. jQuery
If your main goal is to avoid refreshing to update the page you could try remote scripting by using the XMLHttpRequest object in XML.
Give the new CPAN module Yote a try. It binds javascript objects to perl via a JSON rpc.
Here is an example of it on the client :
Server
package Hello;
use base 'Yote::AppRoot';
sub hello {
return "Hello World";
1;
Client
$.yote.init();
var hello_app = $.yote.fetch_app('Hello');
alert( hello_app.hello() );
// outputs "Hello World"