I am trying to store digital signature in database via rails application.
I am using javascript plugin that will convert signature into JSON string. Next I pluck JSON string from params and stored in a variable. Now I want to regenerate the signature by giving the plucked JSON string to a reverse javascript function.
The reverse function works fine if I set a variable in javascript directly to the JSON string and pass it to the function. However it does not work when I assign the variable to my rails variable which stores JSON string. The value of JSON string is exactly the same in both cases.
In first case the typeof var shows obj.obj type correctly but in second case it shows as a string. I tired every trick to parse using JSON and Jquery but it does not work.
My rails controller :
@recipient_signature = params[:recipient_signature]
My view where I want to see generated signature using the JSON string.
var sig = "<%=@recipient_signature%>";
$(document).ready(function() {
$('.sigPad').signaturePad({displayOnly:true}).regenerate(sig);
});
The above case does not work even though the value in @recipient_signature is correctly formed JSON string.
But when I assign the same JSON string to sig like below it works…
var sig = [{"lx":55,"ly":25,"mx":55,"my":24},{"lx":55,"ly":25,"mx":55,"my":25}....'];
This JSON string is same as in @recipient_signature and shows up in <%=@recipient_signature%> correctly.
Javascript somehow does not recoganize it as an array in first case and does in second case… how to fix it?
You’re wrapping your json in quotes and using the variable in html encoded form:
Should be
Or you should parse the json: