Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: perl dancer route template hashref pass complex json file to server issue (module)

by tye (Sage)
on Jul 26, 2016 at 16:28 UTC ( [id://1168578]=note: print w/replies, xml ) Need Help??


in reply to Re^2: perl dancer route template hashref pass complex json file to server issue
in thread perl dancer route template hashref pass complex json file to server issue

Use a JSON library to generate JSON values (we almost always use JSON::XS). Generating a JSON value by hand is error prone, especially when you don't know how escape characters work in quotes in the language you are writing in.

#!/usr/bin/perl -l print qq<{"name":"jsonFileexample","problem":"thisIsThrProblem\"withCo +mma"}>;

for example, produces:

{"name":"jsonFileexample","problem":"thisIsThrProblem"withComma"}

- tye        

  • Comment on Re^3: perl dancer route template hashref pass complex json file to server issue (module)
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: perl dancer route template hashref pass complex json file to server issue (module)
by RamiD (Acolyte) on Jul 26, 2016 at 18:07 UTC
    this is the code ( this time I've tried it :-) ) in my myapp.pm I had
    package myapp; use Dancer ':syntax'; use File::Slurp qw(read_file write_file read_dir); #use db; use JSON; use Data::Dump qw/ pp /; use DBI; our $VERSION = '0.1'; use Dancer::Plugin::Database; get '/test' => sub { my $jsonobj; $jsonobj='[{"name":"test","problem":"here is the problem \" comma +"}]'; debug $jsonobj; template 'test',{passtoserver=> $jsonobj}; };
    under the views I had file test.tt
    <!DOCTYPE html> <html> <head> </head> <body> <div style = "padding: 100px 100px 10px;"> <script> alert('<%passtoserver%>'); var test='<%passtoserver%>'; alert(JSON.stringify(test)); alert(JSON.parse(test)); </script> <button type="text" > </div> </body> </html>
    I had an error with JSON.parse(test) , when I had browser navigated to http://localhost:3000/test yes your right , when we do that with perl module rather than done by hand is a plus , but teh json file was generated by my website user and I'm just store it in DB , all the work on it just post and get from client and server thanks Rami D.
      alert('<%passtoserver%>');

      So, try this:

      alert('"\""')

      What gets displayed? Same as with alert('"""').

      If you are using a templating system to paste values into javascript, then use one that can actually do it correctly, including escaping things that need to be escaped.

      - tye        

        template: "template_toolkit" engines: template_toolkit: encoding: 'utf8'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2025-07-19 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.