I would like to know how I can escape / in angularjs so that I can send it as a path variable to call a restful service. As of now I am getting 404 whenever I try to send a url with path variable values having /; even if I encode it doesn't work.
For example
http://monish.home.com/payment/9a2c1ae67d4ff85e561679fcff/credit/%252B8VMWj/YBC%252FNj3l/fetch/options
Is there a way where I can encode and escape the /?
Yes, its called encodeURIComponent
var a = 'http://monish.home.com/payment/9a2c1ae67d4ff85e561679fcff/credit/%252B8VMWj/YBC%252FNj3l/fetch/options'
encodeURIComponent(a); // result: "http%3A%2F%2Fmonish.home.com%2Fpayment%2F9a2c1ae67d4ff85e561679fcff%2Fcredit%2F%25252B8VMWj%2FYBC%25252FNj3l%2Ffetch%2Foptions"
Related
I wanted to navigate to a URL using queryParams while Routing in Angular.
<a routerLink='/master' [queryParams]="{query:'%US',mode:'text'}"><li (click)="search()">Search</li></a>
The URL I wanted to navigate is:
http://localhost:4200/master?query=%US&mode=text
But when I click on search it navigates me to:
http://localhost:4200/master?query=%25US&mode=text
I do not know why 25 is appended after the % symbol. Can anyone tell me a cleaner way to navigate correctly.
In URLs, the percent sign has special meaning and is used to encode special characters. For example, = is encoded as %3D.
Certain special characters are not allowed in url. If you want to use those in url you have to encode them using encodeURIComponent javascript function.
%25 is actually encoded version of % character. Here browser is encoding them itself.
When trying to get queryParams from url , you can decode them using decodeURIComponent.
For more information check : https://support.microsoft.com/en-in/help/969869/certain-special-characters-are-not-allowed-in-the-url-entered-into-the
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
When I hit the page /login?email=abc+dev#xyz.com
In Ruby's Controller,
The Parameters are: {"email"=>"abc dev#xyz.com"}
+ is missing in this parmas.
What is the correct way to get email in parameters ?
encode URI will give me same(that is, URI::encode("abc+dev#xyz.com") is equals to "abc+dev#xyz.com")
You can use Rack::Utils.escape()
Rack::Utils.escape("abc+dev#xyz.com")
# => "abc%2Bdev%40xyz.com"
Or with Javascript you can use encodeURIComponent()
encodeURIComponent("abc+dev#xyz.com")
"abc%2Bdev%40xyz.com"
When submitting form using get method if we pass # character in any field it skips all parameter after that field.
e.g.
bookmy_car.php?pod=6&room_id=32&starthour=14&startminute=00&startday=07&startmonth=08&startyear=2015&endhour=16&endminute=00&endday=07&endmonth=08&endyear=2015&end_date=1438927200&email_conf=1&cost_code=&desc=Trip description&trip_comment=#&day_rate=68.00&hourly_rate=6.60&hourly_km_rate=0.35&dur_hours=2
hours&location_charge=0.00&damage_cover_charge=5.00&total_free_kms=&longterm=0&rt=&minbooking=3600&returl=&returl_newid=&rep_id=&edit_type=&insPlanid=3&plan_name=goOccasional&id=3&driver_username_id=2&
How do we protect it? I tried escape() and encodeURI() function of JavaScript, it does not help.
I agree with #dgsq . But i prefer using only encodeURI so that he can get the uri as it is in the next page.
alert( encodeURI('&trip_comment=#&day_rate=68.00') )
It happens because with hashbang in query string # it is interpreted as location.hash and hot processed as GET parameters. You need to properly encode URI before you use it. For example with encodeURIComponent:
alert( encodeURIComponent('trip_comment=#') )
I am trying to pass the Euro ( € ) sign as url parameter in my spring jsp. What is the right way to do so ? I tried the following with no avail. Problem is the character is getting encoded properly but not getting decoded from my destination jsp.
I am using
<%#page contentType="text/html;charset=UTF-8" %>
Here is the calling jsp:
<script>
...
// params contains the euro sign
document.location='dest.jsp?p='+escape(params);
In the dest.jsp
<input type="hidden" id="par" value="${param.p}">
and in a script in the same page
console.log($('#par').val())
when I use escape(params) I get the url as %u20AC . But no (empty) values in the dest.jsp
when I use encodeURI(params) or encodeURIComponent I get url as € . But the value in dest.jsp as ⬠- something which I can't use to render as euro sign
I'm going to assume you are using Tomcat because that's what I tested with and we get the same result.
What you will want to do is open up your Tomcat servlet.xml file and find the HTTP connector and add the useBodyEncodingForURI attribute with the value true.
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
redirectPort="8443" useBodyEncodingForURI="true">
</Connector>
Then, you will want to register a CharacterEncodingFilter to set the HttpServletRequest character encoding.
You can read more about this behavior in my answer here:
Character encoding in query string, hebrew
You need indeed to encode the € sign which should give %E2%82%AC using UTF-8. You need to be careful with the encoding you use on both ends.
Something like URLEncoder.encode(url, "UTF-8") on the client would do.
If you are using Spring, org.springframework.web.util.UriUtils has also nice utilities you can use.
If the decoding issue is on the server, you need first to make sure that your web container decodes the URI with the proper encoding.
Tomcat decodes URI with ISO-8859-1 by default so you need to update your connector configuration
<Connector port="8080" ...
URIEncoding="UTF-8"/>
See the following answers
Spring MVC: How to store € character?
Getting question mark instead accented letter using spring MVC 3
I think that org.springframework.web.filter.CharacterEncodingFilter should help here.
Try it with and without your encodeURI(params)
var url = "C:\xampp\htdocs\wcf2\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
results in Uncaught SyntaxError: Unexpected token ILLEGAL. Is there any way to fix it in javascript or do I need to escape it before in PHP?
Edit: My script looks like this:
<script>
window.onload = function() {
thingiurlbase = "{#$__wcf->getPath('td')}js";
thingiview = new Thingiview("viewer");
thingiview.setObjectColor('#C0D8F0');
thingiview.initScene();
var url = "C:\\xampp\htdocs\\wcf2\\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
thingiview.loadSTL(url);
thingiview.setRotation(false);
}
</script>
You need to escape the backslash characters.
var url = "C:\\xampp\\htdocs\\wcf2\\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
Otherwise, JavaScript tries to interpret \x, \h, and \w as escaped special characters.
In this particular case, though, it looks like you could just replace the \ with / instead:
var url = "C:/xampp/htdocs/wcf2/wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
You need to escape backslashes.
var url = "C:\\xampp\\htdocs\\wcf2\\wcf/attachments/5d/18-5dffacdfe6d2cf1db8dfabbb5b53ae8dc65bd325";
If the string is printed from PHP, you can use addslashes to do the work for you.
$test = 'C:\xampp\htdocs\wcf2\wcf/attachments/5d/18-...';
echo $test . "\n";
echo addslashes($test);
>> C:\xampp\htdocs\wcf2\wcf/attachments/5d/18-...
>> C:\\xampp\\htdocs\\wcf2\\wcf/attachments/5d/18-...
Side Note
By the way, two things are incorrect with your approach:
The slashes in your string should all point the same way. If you want it to be used as a URL, use forward slashes e.g. http://stackoverflow.com/posts/17775495
If you want to give the browser a URL that it can use to access some resource on your server, this URL cannot be a file path (such as C:\xampp\htdocs\....) Instead, it has to be either relative to your site root, or an absolute URL (such as http://dropbox.com/my/file.pdf). Don't forget that a web user can only access files on your server through the web server.