'Sending Strings w/Semicolons via Ajax in Rails

I've been using JS/Ajax in place of Rails forms for various things, which has been working great until I tried sending a string containing a semicolon.

Sending a param of...

?string=test;works

is processed by my Controller as...

Processing by Controller#action as */*
  Parameters: {"string"=>"test", "works"=>"nil"}

Instead of what I'd like to see...

Processing by Controller#action as */*
  Parameters: {"string"=>"test;works"}

How can I go about continuing to use JS/Ajax to submit information without strings containing semicolons getting processed like this, and without having to revert to Rails forms for sending strings?



Solution 1:[1]

I was able to get this working by adding the string to the Ajax call's data, rather than concatenating it directly to the URL as I had been doing.

url = '/path'
val = 'test;works'
$.ajax({
  url: url,
  data: {string:val}
});

As opposed to...

url = '/path?string=test;works'
$.ajax({
  url: url
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tristin