Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by RamiD (Acolyte)
on Jul 26, 2016 at 05:36 UTC ( [id://1168535]=note: print w/replies, xml ) Need Help??


in reply to Re: 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

Hi , I will explain more with code example in my perl dancer code I have route
use Dancer ':syntax'; our $VERSION = '0.1'; get '/PopUp' => sub { my $jsonFile={jj =>"{"name":"jsonFileexample","problem":"thisIsThrProb +lem\"withComma"}"} template 'PopUp' ,{'sendfileToserver'=>$jsonFile}; };
in the server side I have the route that receive the perl dancer paramemters
<script> var customjsonList ="<%customCommands%>"; alert(customjsonList.children.length) var output=customjsonList .split(","); alert(output) </script>
so here in javascript I had the error , that some thing is wrong with the json file when I look at the error I saw that json file is missing the \
{"name":"jsonFileexample","problem":"thisIsThrProblem"withComma"}" }
I have perl dancer "Dancer 1.3202" thanks Rami D.

Replies are listed 'Best First'.
Re^3: perl dancer route template hashref pass complex json file to server issue
by Anonymous Monk on Jul 26, 2016 at 07:07 UTC

    Hi , I will explain more with code example in my perl dancer code I have route ... $jsonFile ...

    Sorry, that doesn't compile , its not perl

    $ perl nope Bareword found where operator expected at nope line 5, near ""{"name" (Missing operator before name?) String found where operator expected at nope line 5, near "name":"" Bareword found where operator expected at nope line 5, near "":"jsonFi +leexample" (Missing operator before jsonFileexample?) String found where operator expected at nope line 5, near "jsonFileexa +mple","" Bareword found where operator expected at nope line 5, near "","proble +m" (Missing operator before problem?) String found where operator expected at nope line 5, near "problem":"" Bareword found where operator expected at nope line 5, near "":"thisIs +ThrProblem" (Missing operator before thisIsThrProblem?) Backslash found where operator expected at nope line 5, near "thisIsTh +rProblem\" String found where operator expected at nope line 5, at end of line (Missing semicolon on previous line?) syntax error at nope line 5, near ""{"name" Can't find string terminator '"' anywhere before EOF at nope line 5.

    in the server side I have the route that receive the perl dancer paramemters

    That doesn't mention sendfileToserver

    so here in javascript I had the error , that some thing is wrong with the json file when I look at the error I saw that json file is missing the \

    So far you're not showing a problem with Dancer

    Most likely a problem in your template

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

    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        

      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        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found