Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!"\xampp\perl\bin\perl.exe"
- #
- # jsonp.pl
- #
- # Reads a json file and wraps it in a JavaScript function call
- # One reason to do this is for better decoupling: A JSON file on the server contains the data
- # But the Javascript function to be applied on it belongs to the Web UI (i.e. to the HTML/CSS/JS layer)
- #
- # With the HTML element
- # <script src="status.js?jsonp=show_status"></script>
- # the server will send back: "show_status( <content of status.js> );"
- #
- # Thus, the UI-specific function show_status will immediately be executed on receive
- #
- # Combine this with an internal redirection of query strings in your server
- # In Apache, you may write in your .htaccess
- #
- # RewriteEngine on
- # RewriteCond %{QUERY_STRING} jsonp=([^&]*) [NC]
- # RewriteRule ^(.*)$ /cgi-bin/jsonp.pl?file=$1&jsonp=%1 [PT,L]
- #
- use strict;
- use warnings;
- my %args = ();
- foreach (split /&/, $ENV{QUERY_STRING}) {
- my ($name,$value) = split /=/;
- $args{$name} = $value;
- }
- print "Content-type: text/javascript; charset=utf-8\n\n";
- if (not exists $args{file}) {
- print "Error: No JSON file given in URL path\n";
- exit;
- }
- if (not exists $args{jsonp}) {
- print "Error: No 'jsonp' parameter specified\n";
- exit;
- }
- open( my $JSON,"<$ENV{DOCUMENT_ROOT}/$args{file}");
- if (not $JSON) {
- print "Can't open '$args{file}': $!";
- exit;
- }
- print "$args{jsonp}(\n";
- foreach (<$JSON>) {
- print;
- }
- print "\n);";
- close $JSON;
Add Comment
Please, Sign In to add comment