Using ‘&’ in POST variables [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ajax post special characters
I am building my own post parameters and passing them via ajax; however, my POST contains an & symbol. How can I post these and escape the special characters so it posts as text only and doesn't split up my value?
Ex:
Thing=lala&lalala
Should be thing = 'lala&lalala' but what I get is thing = 'lala' and ='lalala' where the second key is blank.

You should use encodeURIComponent on your parameters before sending them to the server. It will properly escape everything in your parameters.
Quick example:
var s = 'Thing=' + encodeURIComponent('lala&lalala');
Please note that every value should be encoded separately (so you should not simply use it on the whole query string).

Related

can '?' separator be included twice in the URL [duplicate]

This question already has answers here:
Is it valid to have more than one question mark in a URL?
(2 answers)
Closed 1 year ago.
https://www.websitename.com/?audience=testingting?internal-abhishek-jwt=random_string
Is this a valid URL? i.e is it fine to have two '?' separators in the URL?
Note: Due to limitations, I can not add '&' to internal-abhishek-jwt=random_string
It isn't. You have to URL encode the second one if you want to use it as a parameter or use '&' symbol instead if you wanna separate your arguments (which seems to be the case here).
Some further explanation here
The ? still indicates the start of the query string.
Segments of the query string are separated by & characters.
This will be parsed as:
key: audience
value: testingting?internal-abhishek-jwt=random_string

Getting string from another string in Javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 2 years ago.
Please note that this is not a json object :) My string is:
{"message":"***error in SAP module:-1***","status":400}
This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.
So, I want to take only the bold string (all the value of "message").
I thought about lastIndexOf, and pick the string between ":" and ","
But I got messed up with the escape characters for the quotes.
How can I achieve it with lastIndexOf? Or in another better way?
If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "

Creating a URL (and not necessarily a parameter list) using jQuery or JavaScript [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 6 years ago.
I am communicating to a REST server which is expecting something like http://example.com/1.0/editor/5/bla/26 along with body parameters of x, y, and z. Or maybe instead of /1.0/editor/5/bla/26, it should be 1.0/editor/5/bla/what about item #26, but of course with what about item #26 escaped.
There are many post describing how to use jQuery.param() to encode parameters, however, that is not my question. How chould the actual url be created using jQuery of JavaScript?
$.ajax({
type:'PUT',
url:'/1.0/editor/'+$('#id').val()+'/bla/'+$('#wha').data('id'),
data:{x:x,y:y,z:z},
success: function (error){}
});
Can you write a javascript RegEx function to replace all spaces with a known character? Or replace it with its HTML equivalent. Then in your backend, just look for that escape character and replace them with the spaces again. It's kind of a work around but depending on your situation it could work.

Is there any way to return the thrown away part of the split operation? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
I'm building a basic application which lets the user to share or continue their search status between sessions with # parameters in Javascript (I'm building this as a SPA so GET parameters won't always necessarily work).
In my app I could have a URI like: /items#rarity=rare,common,uncommon&cost=ascending&category=primary
If I wanted to check what state to set the cost filter in my react component, I'd want to extract asecnding and then check the ascending checkbox to set the state on page load.
If I use Javascript's split function combined with regex, I can capture all information on cost by doing:
var hash = window.location.hash;
hash = hash.split(/cost.*&/);
Right now, this will obviously return an array in two parts the first being /items#rarity=rare,common,uncommon& and category=primary as the split function will split on the condition supplied which in my case matches the string from regex.
Is there any way I can capture the extracted string from the split function so I can then parse the cost string?
No way. Oh, there is! Just wrap your regex in the capture group.
var s = '/items#rarity=rare,common,uncommon&cost=ascending&category=primary';
r = s.split(/(cost.*&)/);
console.log(r[0]);
console.log(r[1]); //there is the "thrown" part
console.log(r[2]);
To achieve expected result, use below
var x="/items#rarity=rare,common,uncommon&cost=ascending&category=primary";
console.log(x.split('cost=')[1].substring(0,x.split('cost=')[1].indexOf("&")));
http://codepen.io/nagasai/pen/RRyLmA

Other delimiters are used (not & and =) when submitting a form [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get query string values?
How to change how the URL is encoded when a form is submitted?
I have the site where I have the form and When i push the button's send.
While sending I want to program the submit is form because i want to use the other delimiter & and =. This is possible?
Now I write one example:
www.theuser.com/?Name=IBM&surname=PC
With if the function programable while sending
www.thepc.com/?Name%24+IBM+%23%+Surname%24+PC
Repeat:
when I sent the parameter, I not want this separator & or = and I want to use the others.
This Query String is the original for the my project:
http://localhost:8100/index2?json_data=demo_title%24+Demo+title+%23+proc1_script%24+script.sh+parameters+%23+proc1_chk_make%24+on+%23+outputp2_value%24++%23+demo_input_description%24+hola+mundo+%23+outputp4_visible%24+on+%23+outputp4_info%24++%23+inputdata1_max_pixels%24+1024000+%23+tag%24++%23+outputp1_id%24+nanana+%23+proc1_src_compresion%24+zip+%23+proc1_chk_cmake%24+off+%23+outputp3_description%24++%23+outputp3_value%24++%23+inputdata1_description%24+input+data+description+%23+inputp2_description%24+bien%3F+%23+inputp3_description%24+funciona+%23+proc1_cmake%24+-D+CMAKE_BUILD_TYPE%3Astring%3DRelease++%23+outputp2_visible%24+on+%23+outputp3_visible%24+on+%23+outputp1_type%24+header+%23+inputp1_type%24+text+%23+demo_params_description%24+va+bien+%23+outputp1_description%24++%23+inputdata1_type%24+image2d+%23+proc1_chk_script%24+off+%23+demo_result_description%24+win%3F+%23+outputp2_id%24+nanfdsvfa+%23+inputp1_description%24+funciona+%23+demo_wait_description%24+boh+%23+outputp4_description%24++%23+inputp2_type%24+integer+%23+inputp2_id%24+papapa+%23+outputp1_value%24++%23+outputp3_id%24+nananartrtrt+%23+inputp3_id%24+pepepe+%23+outputp3_type%24+header+%23+inputp3_visible%24++off+%23+outputp1_visible%24+on+%23+inputdata1_id%24+id_lsd+%23+outputp4_value%24++%23+inputp2_visible%24+on+%23+proc1_source%24+lsd-1.5.zip+%23+inputp3_value%24+si+%23+proc1_make%24+-j4+-C++%23+images_config_file%24+cfgmydemo.cfg+%23+outputp2_type%24+header+%23+proc1_subdir%24+xxx-1.5+%23+proc1_url%24+http%3A%2F%2Fwww.ipol.im%2Fpub%2Falgo%2F...+%23+inputdata1_image_depth%24+1x8i+%23+inputp1_id%24+popopo+%23+inputp1_value%24+si+%23+inputp2_value%24+no+%23+demo_data_filename%24+data_saved.cfg+%23+inputdata1_info%24+info_lsd+%23+outputp3_info%24++%23+inputdata1_image_format%24+.pgm+%23+outputp1_info%24++%23+inputdata1_compress%24+False+%23+inputp1_visible%24+on+%23+proc1_id%24+lsd+%23+outputp4_id%24+nana+%23+outputp2_description%24++%23+outputp4_type%24+header+%23+outputp2_info%24++%23+inputp3_type%24+float+%23+&tag=&inputp4_id=hi&inputp4_type=text&inputp4_description=hello+program&inputp4_value=no&inputp4_info=bol&inputp4_visible=on
tldr; The encoding is "correct"; however ..
.. something incorrectly percent encoded the full URL.
Only encode the components (keys and values), not the full URL or gibberish like this will result.
The = and & characters are valid reserved characters in the query portion of a URL and should not be encoded.

Categories