encode/decode Query String ReturnUrl Parameter - javascript

I am sending the return url as a query string parameter like this http://localhost:50316/TripNestFinal/Login.aspx?ReturnUrl=~/Account/AccountSettings.aspx
But i want ~/Account/AccountSettings.aspx to be encoded in such a way so that i can
encode/decode in jQuery/javascript
encode/decode in VB.NET
encode in VB.NET and decode in javascript/jQuery and vice versa
This doesn't have to be bulletproof as i am not dealing with security here. All i want to do is to change this ~/Account/AccountSettings.aspx to something that does not show the path directly.
I thought of using Base64 encoding but when i Base64 Encode , it includes a '/' character which breaks my jQuery logic. Is there any way i can avoid the '/' character when i Base64 encode?

You will need to URL encode the Base64 encoded string, that will work - although you'll need to check the generated query string length to make sure it doesn't hit the .NET maximum.
But I have to ask why you'd want to encode it in this way?

Related

Send basa64 string to webService

What is best way to send data from html to webService? My base64 string is very long (5000 character and more).
I'm use ajax but not working for very long string.
Any solution?

javascript chinese/japanese character decoding

I created a JSONP function on the server and returns a UTF-8 encoded json object like this
applyLocalization({"Name":"%E5%90%8D%E5%89%8D","Age":"%E5%B9%B4%E9%BD%A2"});
on my javascript on the client side, i want to convert the garbled part to their original state like
{"Name":"名前", "Age":"年齢"}
I tried $.parseJSON() but it doesnt work
You can use decodeURIComponent to decode urlencoded strings like yours
decodeURIComponent('%E5%90%8D%E5%89%8D');
//result: '名前'
You could use the decodeURIComponent function. But you shouldn't be URL encoding your javascript strings. You should send them as UTF-8 strings as-is. Javascript is capable of understanding them.

Functions that work the same in encode URL in Javascript and PHP?

I've been looking for the functions [used to encode and decode URL] that can be exchangable between PHP and Javascript [I can encode the URL in javascript and decode it in PHP and vice versa], there are many solutions come up, like escape() and unescape(); encodeURI() and decodeURI() for javascript, or urlencode() and urldecode() for PHP, but there is no pair which can works together perfectly. Because I want to send post data using Ajax jQuery for a PHP page to use, and data can be mixed with special character, that's why I need a function that works the same in PHP and Javascript:
Example:
$.ajax{url: "test.php", data: "http://www.thisURLContainSpecialCharac.ter"}
See the code above? It contains special characters in data, that may cause problem.
I appreciate any advice :). Thanks.
[x]
You don't want to use Javascript's escape function since it does not URL encode. See http://xkr.us/articles/javascript/encode-compare/ for examples where it fails.
Javascript's encodeURIComponent will encode assuming UTF-8, so if you use that and make sure that your request has a content-type of UTF-8, you should be able to decode in PHP using
utf8_decode(urldecode($yourString)).

ajax post special characters

How do I pass a a large string containing characters like '%' and '&'
to my php page through ajax post?
In other words how to javascript-encode them and php-decode them?
the encodeURIComponent() JavaScript function can be used to escape any of those characters in either the keys or the values.
PHP will receive and decode it automatically into the $_POST array.
The format of the data should be Query String format, specifically:
Content-Type: application/x-www-form-urlencoded
For example:
Name=Joe&Age=23&City=Altoona
If you use encodeURIComponent() on each key and each value, then join them with =, and group them by &, you won't have any further issues.
To encode them in javascript, use encodeUri and encodeUriComponent
http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp
PHP Decode:
http://php.net/manual/en/function.urldecode.php
You actually don't need to encode or decode something, if you use POST parameter in send method of XMLHttpRequest. Only GET needs such and encoding, since it uses URI field.
You should be careful when using this data in SQL requests. Beware of injections.

What is the recommended way to pass urls as url parameters?

Using &url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
What is the recommended way to pass urls as url parameters ?
encodeURIComponent() should work. For example,
'&url=' + encodeURIComponent("http://a.com/?q=query&n=10")
produces
"&url=http%3A%2F%2Fa.com%2F%3Fq%3Dquery%26n%3D10"
(which doesn't have any & or ? in the value).
When your server gets this url, it should be able to decode that to get the original:
param["url"] = "http://a.com/?q=query&n=10"
I'm not sure what server you're using (e.g. Rails, Django, ...) but that should work "out of the box" on any normal system.
Using '&url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url
Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string.
but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
Then the server is very broken indeed. If that's really what's happening, you need to fix it at the server end.
Code?
I ran into this issue, personally I couldn't use any of the accepted answers, but it can also be done by just encoding the url into Base 64, passing it as a parameter, and then decoding it. With javascript, you can encode a string s to base 64 with btoa(s) and decode with atob(s).
Other languages have ways of doing the same thing.
Base 64 is just kinda like representing a larger series of characters with 64 character(For ex, all the capital letters, all the lowercase, and a couple symbols). Kinda like how we represent letters in Binary. But it's nice to use, because then we can just pass base64 strings as parameters, and then they won't interfere/get interpreted in a weird fashion, and then we can decode them at the next stage.
Use escape() to url encode it, it will encode the ampersands so that does not happen.
Honestly, go with Google URL Shortener. Then you can just use the URL code in the url query string: http://example.com/url/A7dh3
In your application, take that and prepend the Google URL Shortener domain name, and do the redirect. This adds tracking to the URL through Google Analytics also. Lots of advantages in this approach. Just a short code and added tracking data, too.

Categories

Resources