URL Params with Special Characters in Jquery laravel

Viewed 572

As of now I can access my url params using this code

acl params contains special character like + where the value of this param is

Very High 15% +

where In my browser looks like this

link

http://localhost/whitelabs/yeast-bank?categoryID=3&searchType=YEAST&keyword=a&acl=Very%20High%2015%%20+

JS

 $( "#btnSearchYeast" ).click(function() {
        var keyword = $("#keyword").val();
        var categoryID = $( "#selectType2" ).val();
        var acl = $( "#selectType3" ).val();
        location.href="{{ url('yeast-bank') }}"+"?categoryID="+categoryID+"&searchType=YEAST&keyword="+keyword+"&acl="+acl;
        $( "#compareDiv" ).hide();
    });

But when I tried to dd($acl); on my controller.

It returns something like this

enter image description here

Where + sign is missing now in my controller. How can I get the value of acl param as is?

2 Answers

You need to use encodeURIComponent to encode your string, as well as decodeURIComponent on the other end:

const myString = "here + is a % dangerous & string"
const encodedString = encodeURIComponent(myString)
console.log(encodedString);
const decodedString = decodeURIComponent(encodedString)
console.log(decodedString)

you need to encode this to URI-Format

the correct coded Version is

encodeURIComponent( "Very High 15% +")

== 

"Very%20High%2015%25%20%2B"
Related