How to decode part of an Ajax call QueryString in c# - javascript

I am encoding the query string in an Ajax call in Javascript using encodeURL() but I can't find a way to then decode it on the serverin c#.
I use HttpContext.Current.Request["ID"] to get the parameter values from the query string and in the debugger I can see that Request.QueryString is fully encoded but when I try to get the value of a particular parameter it wont Decode.
I have tried using HttpUtility.UrlDecode(HttpContext.Current.Request["ID"]) but it doesn't appear to decode it. What am I missing?
Or should I be using a completely different way too get the query values?

Ah sorry!!! - sometimes you just need someone else to ask you a question and then you see the answer yourself - on the client I was using encodeURI instead of encodeURIComponent.

Related

Angular JS is not encoding JSON correctly to pass with get request?

I am trying to send a JSON string along with the url ass http get using angular's $http service but somehow the curly braces are being removes when the request is sent because of may be angular's URI encode function is not working correctly, is there any work around for this? the samlpe would be if I send
http://someurl.com?a={"a":"b"}
it is sent to server as
http://someurl.com?a="a":"b"
I dont know whats wrong with Angular.
Use JSON.stringify to convert your url object into a JSON text. Then use encodeURIComponent(JSON_text) to encode the url.
It is to do with URI encoding.
Take a look at the following question for your answer:
How to escape a JSON string to have it in a URL?
;)

IE : window.open url which is more than 2,083 characters

I am getting a url from server with lot of data as query string
(E.g. http://www.test.com/?n=1,2,3,4,5,6,7,8.....100000) and I want to open it using window.open().
But the moment I pass the URL to window.open the url which gets gets truncated. After searching for sometime I could figure out that the maximum limit for URL is 2,083 characters(IE) so it passes PART of query string and truncates the rest..
How can I overcome this?
Please let me know if I need to provide more details.
I think the only solution is to use POST instead of GET. Just use a form, instead of window.open. Please see this older answer:
https://stackoverflow.com/a/17089124/907420
You could try URL shorteners, like goo.gl or bit.ly:
https://goo.gl/
Speaking of programming, you could try to shorten your URL-s yourself, for the given example:
Exact URL for your example, shorter by ~2000 characters.
Where x..y is translated on server side as range(x, y) -> 1,2,3,4,5...100000 for x and y being 1 and 100000
If you want to stick with GET (and I would recommend that you use POST) you can try to compress the parameters. Instead of giving a huge number of parameters you create a javascript object holding the parameters, jsonfy it and you end up with a string that can be compressed and uncompressed again at the other end. Afterwards you can deserialize the JSON string and you have your parameters. Depending on the number of parameters it might still not be sufficient for a GET request.
But at the end a POST request is the best solution I think.

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)).

Encoding user input as URL GET parameter in JavaScript

I'm writing a JavaScript function that needs to uphold three properties:
be very small and lightweight - no external libraries
encode a string in such a way as to be able to be passed as a GET parameter
this string must be decoded again at its destination
Effectively, it authenticates the user by sending his username and password to a PHP page which then verifies it. This is done via GET because I haven't yet found a way of doing a background cross-domain POST request. The trouble is that if the user has a character such as '#' or similar in his password, it doesn't get sent properly.
Currently to avoid this, I encode() the password string before sending it, which allows it to be received without problems. However, I read that PHP's urldecode() is not a perfect analog for this, as there are corner cases which are treated differently (i.e. ' ', '+', etc). Sadly I cannot find this document anymore, so I cannot quote it, but the gist was that one of them converts spaces into '+' signs, which the other treats as an actual plus sign, or something like that...
As such, I'm looking for a Javascript function that can take a string and make it URL-safe, and which has a perfect reversal function in PHP so that the original string can be recovered.
The arguably awful code I currently use to achieve this:
login.onsubmit = function(){
loginFailMsg.style.display = 'none';
var inputs = login.getElementsByTagName('input');
var formdata =
'username='+inputs[0].value+'&password='+encode(inputs[1].value);
submit.src = formtarget+'/auth/bklt?'+formdata;
userinfo = undefined;
setTimeout(getUserinfo,300);
return false;
};
encodeURIComponent, PHP will decode it automatically when populating $_POST or $_GET
'&password='+encode(inputs[1].value)
Where's encode function coming from? Seems to me the quick answer to your question is using encodeURIComponent() instead, available since JavaScript 1.5. See also Comparing escape(), encodeURI(), and encodeURIComponent(); it does not encode everything either, but does encode all the server expects it to.
(As for cross-domain AJAX POST calls, I'd really have a look at "JSON with Padding". See JSONP with jQuery that I mentioned in the comments earlier. This will also prevent issues with the timeout you've randomly chosen, and jQuery will also help you, a lot, to get rid of inputs[0].value and the like. And, as you apparently already have a MD5 hash on the server, I'd really hash the password client side as well --see Karl's answer-- and compare those hashes instead. Respect your user's password and your own time, drop that no external libraries requirement!)
I don't think there's such a thing as a reversible hash function. There are plenty of javascript md5 libraries available, however.

How to pass HTML code in query string

I need to pass html code with QueryString because im using ajax method to send post(i mean comment not the type of method) on my web site. When I write a post like that.
"Hi everybody<br />Whats'up."
its just taking "Hi everybody" removing rest of the content.
Info : Im using GET Method
Don't use escape, it's a deprecated function. Use encodeURIComponent instead:
encodeURIComponent("Hi everybody<br />Whats'up.");
Also, don't forget about Internet Explorer's 2,083 character limit in the address bar, you should use POST instead of GET if you want to avoid it.
What method are you using - GET or POST? You should be using POST. That will allow you to send full html and you don't need to use the querystring.
http://www.mywebsite.com/index.html?html_code=Hi%20everybodyWhats'up.

Categories

Resources