Levent Tulun
Levent Tulun

Reputation: 711

How to escape slash in URL parameter

$.ajax({
        url: "/api/v1/cases/annotations/" + case_id + "/" + encodeURIComponent(current_plink),

I am using encodeURIComponent to escape slash. But it does not work for me. This code convert "slash" to "%2F" but apache does not recognize it.

My php part like this :

$app->get('/cases/annotations/:case_id/:prep_name', 'authenticatePathologist', function($case_id, $prep_name) use ($app) {

If i try to send parameter which include slash, it returns page not found. enter image description here

But if i try to send parameter which does not include slash, it returns OK. enter image description here

Upvotes: 8

Views: 19873

Answers (3)

Kailas
Kailas

Reputation: 7578

I too had a similar issue with url like:

?filters=new:item:text

where it required to escape the colons. for that I tried replacing it to format ?filters/:items/:text but that couldn't work as the Chrome was NOT encoding the '/' value.

Tried the js [escape() method][1] and it worked like charm.

let url = 'https://dev.test.something?filters=';
let filterString = 'new:item:text';
url = url + escape(filterString);

Output:

https://dev.test.something?filters=new%3Aitem%3Atext

Hope this finds helpful!!

Upvotes: 0

Michał Perłakowski
Michał Perłakowski

Reputation: 92521

You should encode it twice with encodeURIComponent, i.e. encodeURIComponent(encodeURIComponent(current_plink)). If you encode it only once, the server decodes it, and it's the same as not encoding it at all.

Upvotes: 25

Vijay Dohare
Vijay Dohare

Reputation: 741

you should follow AllowEncodedSlashes Directive

Upvotes: 0

Related Questions