Pass json with special characters angularjs to ruby

Issue

I need to make a search form, where the back end used is ruby and front end is angular. the search query is generated in angular in json format and is passed to ruby via restangular service.

Its working fine. But when we tested the search string with semicolon it returned 500 Internal error. The first line puts params[:search] gives {"content":"my search is for the actual json string {"content":"my search is ; and :", "options":[]}

Please help me on how to handle it “;” also please let me know what all characters I need to be handled.

Solution

You need to encode your params first.

So the encoded params will be:
{"content":"my search is %3B and :", "options":[]}

The following is also valid:
%7B%22content%22%3A%22my+search+is+%3B+and+%3A%22%2C+%22options%22%3A%5B%5D%7D

You can encode URL using: https://www.w3schools.com/tags/ref_urlencode.asp

I am sure Angular will must have some library to encode the URLs.

You could also use native Javascript URL encoder.

The similar question was already answered on SO.

Answered By – Kartikey Tanna

Answer Checked By – Dawn Plyler (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.