How to escape slash in URL parameter - javascript

$.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.
But if i try to send parameter which does not include slash, it returns OK.

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.

you should follow
AllowEncodedSlashes Directive

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!!

Related

prevent javascript to encode url

I am trying to change url with pushstate in java script and my url doesn't have any space or bad character for url but java script encode it and add some character in it.
my code is:
name= name.trim();
const nextState = { additionalInformation: name };
window.history.pushState(nextState, "", my_domain()+"/brands/" + name);
my url is:
http://localhost/brands/Brilliance
but it show as:
http://localhost/brands/Brilliance%E2%80%8C
The '%E2%80%8C' at the end of your URL is an invisible Unicode/ASCII character that you are likely copying when you have pasted in the URL, or maybe a package is causing it. In either case, here are two ways you can solve this:
You can paste your link into a hex editor and remove the invisible character manually before copy-pasting back into your code editor.
You can use this javascript solution to remove the characters:
function remove_non_ascii(str) {
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace(/[^\x20-\x7E]/g, '');
}
console.log(remove_non_ascii('äÄçÇéÉêHello-WorldöÖÐþúÚ'));
The provided code has no reason to not work, but I have a suspicion that in fact name is malformed. If I get the %E2%80%8C suffix in the URL and run it trough decodeURI, I get an empty string. However, if I manually convert it to the string \xe2\x80\x8c, I get the following: â\x80\x8C. This most probably means corrupted data.
Change your URL using encodeURIComponent
const nextState = encodeURIComponent('http://localhost/brands/Brilliance');
history.pushState({}, '', nextState);
This will change your url and the URL will not have any spaces and bad characters

Slash interpolation in JS

I have got the following url:
'/aaa/bbb/ccc/' + part + '/ddd/eee'
part is variable which contains '/'. I need to interpolate it, because Rails backend catches this route as incorrect (404 error), but part value must be just part or URL. How can I do it? Thanks in advance!
You need to encode this url to make it valid url. Secondly I think you should pass this part as query parameter. Apparently you can do something like
'/aaa/bbb/ccc/' + encodeURIComponent(part) + '/ddd/eee'
You can pass it as query parameter like
'/aaa/bbb/ccc/ddd/eee?part='+encodeURIComponent(part)

javascript how to encode url,which has '-'

I'm trying to pass parameter to a URL like this:
bike-my-myRecharge-dailyAccount-detail.html?billTime=2016-02
but the URL I get is always:
bike-my-myRecharge-dailyAccount-detail.html?billTime=2013
I've tried encodeURIComponent() and encodeURI(), but it doesn't work.
I've tried encodeURIComponent() and encodeURI(), but it doesn't work.
Can you please post the error that you are seeing.
//test url
var uri = "my test.com?bike-my-myRecharge-dailyAccount-detail.html?billTime=2016-02";
var res = encodeURI(uri);
- will be encoded to %2D
You can directly test your encoded url in W3C

Encode url but keep working

Weird one but is it possible to make Link work without the text helloworld.com being in the source?
Long story but I have function breaking helloworld because it matches it and I remember back to my AOL days where you could "encode" a url to look completely different text but it still took you to that url? Note I dont mean encoding the ? or = etc - but the actual text of domain name... have I completely imagined this?
Weird one but is it possible to make Link work without the text helloworld.com being in the source?
To what end? If you want to totally prevent users from figuring out the URL before they click the link, no that's impossible. If instead you just want to prevent that URL from being picked up by scrapers that don't run JavaScript, then yes that is entirely possible.
What I would do in that case is add a data attribute to the link with an encoded version of the URL.
Link
Then in your JavaScript, decode. Untested jQuery example to get you started:
$('a[data-href-rot13]').each(function (index, el) {
$(this).attr('href', rot13($(this).attr('data-href-rot13')));
});
Of course, you might want a different algorithm than rot13. You could use AES and base-64 encode the output for use in the tag. In case you do want rot13, see this post: https://stackoverflow.com/a/15747894/362536
You can url encode your URL:
(here just on the 'l' character)
Hello World !
It's not an encryption, but a simple encoding. Url cannot use all characters inside of them, so special chars can be passed with the form %XX where XX is the hexadecimal representation of the char.
You browser will decode it, before using it. This coding is normally seen after the url for params and path (ie: url.com/this%20space), but it is also available inside the domain name.
You can encode an url like that with this short javascript:
var encodeUrl = function(url) {
return url.split('//')[0] + '//'
+ url.split('//')[1]
.replace(/[a-z ]/gi, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
url = 'http://www.helloworld.com/';
encodedUrl = encodeUrl(url)
document.body.innerHTML = 'Raw url: <pre>' + url + '</pre>EncodedUrl: <pre>' + encodedUrl + '</pre>Result: TEST';

Encoding URL in JavaScript

I'm unable to encoding data URI:
var uri ="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQo...";
var res = encodeURI(uri);
document.location.href = 'display.jsp?img='+res;
After encoding, I'm getting the same uri. display.jsp is landing as am empty page.
There is no encoding happening because what you have there is already a valid, completely encoded URI.
If you want to use that as a parameter in an other URI, you should use encodeURIComponent:
document.location.href = 'display.jsp?img='+encodeURIComponent(uri);
It is not correct to use encodeURI() as this function encodes special character except: , / ? : # & = + $ #
Use encodeURIComponent() to encode these characters.
For more info check below link:
http://www.w3schools.com/jsref/jsref_encodeuri.asp
Your problem is that the encodeURI function is for making a URL valid for a browser, not for formatting content into a URL (which is what you're doing). A base64 string is already formatted in such a way that it registers as valid. To encode it as part of the URL, you need to use encodeURLComponent.
Basically, just use:
var uri ="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQo...";
var res = encodeURIComponent(uri);
document.location.href = 'display.jsp?img='+res;
For more info, check out: When are you supposed to use escape instead of encodeURI / encodeURIComponent?

Categories

Resources