I am facing 1 issue with localStorage
I am storing the value like below in localStorage using Set
elibom!%5E!fyzqrutc5%3b47<47568%255>%3f8<%3f5%3a
and passing that param to the ajax call but in browser console its replacing with the following
elibom!%255E!fyzqrutc5%253b47%253C47568%25255%253E%253f8%253C%253f5%253a
Whats the issue is this how to fix it I know its decoding the string but how to fix this
You can use encodeURIComponent/decodeURIComponent to solve this issue.
encodeURIComponent("elibom!%5E!f<%")
// -> "elibom!%255E!f%3C%25"
decodeURIComponent("elibom!%255E!f%3C%25")
// -> "elibom!%5E!f<%"
Before handing your string over to the AJAX call, do the encoding (don't use encodeURI - this will not replace &, +, and =) and after that, decode again.
Related
I am facing some issues with escaping of back slash, below is the code snippet I have tried. Issues is how to assign a variable with escaped slash to another variable.
var s = 'domain\\username';
var options = {
user : ''
};
options.user = s;
console.log(s); // Output : domain\username - CORRECT
console.log(options); // Output : { user: 'domain\\username' } - WRONG
Why when I am printing options object both slashes are coming?
I had feeling that I am doing something really/badly wrong here, which may be basics.
Update:
When I am using this object options the value is passing as it is (with double slashes), and I am using this with my SOAP services, and getting 401 error due to invalid user property value.
But when I tried the same with PHP code using same user value its giving proper response, in PHP also we are escaping the value with two slashes.
When you console.log() an object, it is first converted to string using util.inspect(). util.inspect() formats string property values as literals (much like if you were to JSON.stringify(s)) to more easily/accurately display strings (that may contain control characters such as \n). In doing so, it has to escape certain characters in strings so that they are valid Javascript strings, which is why you see the backslash escaped as it is in your code.
The output is correct.
When you set the variable, the escaped backslash is interpreted into a single codepoint.
However, options is an object which, when logged, appears as a JSON blob. The backslash is re-escaped at this point, as this is the only way the backslash can appear validly as a string value within the JSON output.
If you re-read the JSON output from console.log(options) into javascript (using JSON.parse() or similar) and then output the user key, only one backslash will show.
(Following question edit:)
It is possible that for your data to be accepted by the SOAP consuming service, the data needs to be explicitly escaped in-band. In this case, you will need to double-escape it when assigning the value:
var s = 'domain\\\\user'
To definitively determine whether you need to do this or not, I'd suggest you put a proxy between your working PHP app and the SOAP app, and inspect the traffic.
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 wanna pass %20 in Query string. But in code behind it will considered as space in code behind.
How can i do that?
I am passing querystring using asp.net and also from javascript.
Please help me out for the same. Thanks in advance.
When you pass value from Asp.net code behind use Server.UrlEncode(%20)
When you pass value from JavaScript use encodeURIComponent('%20') (which will yield %2520)
Pass %2520 to escape your %, that's how.
I'm working to integrate some code with a third party, and sometimes a string argument they pass to a Javascript function I'm writing will be encoded using encodeURIComponent, sometimes it won't be.
Is there a definitive way to check whether it's been encoded using encodeURIComponent
If not, I'll do the encoding then
You could decode it and see if the string is still the same
decodeURIComponent(string) === string
Not reliably, especially in the case where a string may be encoded twice:
encodeURIComponent('http://stackoverflow.com/')
// yields 'http%3A%2F%2Fstackoverflow.com%2F'
encodeURIComponent(encodeURIComponent('http://stackoverflow.com/'))
// yields 'http%253A%252F%252Fstackoverflow.com%252F'
In essence, if you were to try and detect the string encoding when the passed argument is not actually encoded but has qualities of an encoded string, you'd be decoding something you shouldn't.
I'd recommend adding a second parameter in the definition "isURIComponent".
However, if you wanted to attempt, perhaps the following would do the trick:
if ( str.match(/[_\.!~*'()-]/) && str.match(/%[0-9a-f]{2}/i) ) {
// probably encoded with encodeURIComponent
}
This tests that the non alphanumeric characters that don't get encoded are intact, and that hexadecimals exist (e.g. %20 for a space)
I have a javascript variable comming from legacy system with backslashes into forward slashes:
'/46\465531_Thumbnail.jpg'
and I am trying to convert into this:
'/46/465531_Thumbnail.jpg'.
There is no way to fix the problem on the legacy system.
Here is the command I am running on IE8 browser:
javascript:alert("/46\465531_Thumbnail.jpg".replace(/\\/g,"/"));
as response I get:
---------------------------
Message from webpage
---------------------------
/46&5531_Thumbnail.jpg
---------------------------
OK
---------------------------
actually I just want to be translated as '/46/465531_Thumbnail.jpg'
What is wrong?
You need to double the backslash in your string constant:
alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
If your legacy system is actually creating JavaScript string constants on your pages with embedded, un-quoted (that is, not doubled) backslashes like that, then it's broken and you'll have problems. However, if you're getting the strings via some sort of ajax call in XML or JSON or whatever, then your code looks OK.
It is actually interpreting \46 as an escape-code sequence for the character &. If you are going to hard-code the string, you need to escape the \:
alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
^^ change \ to \\
Sample: http://jsfiddle.net/6QWE9/
The replacement part isn't the problem, it's the string itself. Your string:
"/46\465531_Thumbnail.jpg"
isn't /46\465531. Rather, the backslash is acting as an escape character. You need to change it to:
javascript:alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
ie, escapeing the backslash with a backslash.
Nothing wrong with the replace. The input is wrong.
javascript:alert("/46\\465531_Thumbnail.jpg".replace(/\\/g,"/"));
^
\---------------- need to escape this!