Encoding URL into ASCII using javascript - javascript

I am writing a php script-wrapper for my API encode/decode requests and responses. The php script works fine with forms but I can't send properly encoded requests using javascript (angular)
In details:
I have an encoded request (on client side):
{"ct":"2UExUFiKFiZeT73AbLWYrCzJZnYUuCaaLq6VKcaboj/VQxDIrRXa6b4FvbVJXgU9","iv":"5c37e23d740ceb7611cb707bacd66633","s":"11bd03dda58ffbbe"}
The form converts this request into
%7B"ct"%3A"2UExUFiKFiZeT73AbLWYrCzJZnYUuCaaLq6VKcaboj/VQxDIrRXa6b4FvbVJXgU9%3D%3D"%2C"iv"%3A"5c37e23d740ceb7611cb707bacd66633"%2C"s"%3A"11bd03dda58ffbbe"%7D
It works, but I need to replicate it using angular http.get()
http.get doesn't convert this request properly. I tried to use encodeURI() but it doesn't help, obtain:
"%7B%22ct%22:%222UExUFiKFiZeT73AbLWYrCzJZnYUuCaaLq6VKcaboj/VQxDIrRXa6b4FvbVJXgU9%22,%22iv%22:%225c37e23d740ceb7611cb707bacd66633%22,%22s%22:%2211bd03dda58ffbbe%22%7D"
It converts quotes, what based on the previous example I don't need
Do you have any ideas?

Related

javascript - send ajax request without string concatenation

I have a page in wich the user will write a code (in a programming language). the code can be very long (more than 2000 characters) and can contain any type of characters. And when he press the send button the code will be sent (using javascript ajax) to a script file on the server etc..
currently i am using this to send the code with ajax :
jxobj.open('POST', 'www.my-website.com/my_script.php', true);
jxobj.send('code=' + code);
but if the code contains some characters like & then the value of code will be broken into many pieces and i can't retrieve it in my script file.
Is there any way to send ajax request without a string concatenation? I would like to use only javascript if possible and thanks.
The data format is well documented and the standard JavaScript encodeURIComponent function will encode data for it.
jxobj.send('code=' + encodeURIComponent(code));
Is there any way to send ajax request without a string concatenation?
Not really. The send method takes a string, so you have to build a string using some mechanism or another. You could achieve it with (for instance) arrays and joins, but it still comes down to the same basic principles.

send unescaped string with XHR (javascript)

I have string containing javascript file content and I need to upload it to server (it's not part of server side code - it's used only for development (on-site code editing) process so security is not important). PHP script uses file and content variables. I found only implementations where xhr.send() argument was string in standard GET format (var1=sth&var2=sthelse) but I'd like to send this string without any encoding / decoding / escaping / unescaping on server side. As it's js it contains all possible characters including '&' or '='.
Is it possible to pass data to POST in other way than using standard query?
I'm rather not interested in jQ solution
Sending JS file contents in a query string parameter is not a good idea. A few points:
If your situation support HTML5 you can send files to your server with JS.
If you don't want to send as a file upload I would send it as POST data.
Any data encoded or escaped on the client can be decoded or unescaped on the server. In some setups this is automatically done for you. There's no reason to avoid this if it's the proper way to handle data.

Encodeuricomponent decoding it in rails

Normally rails magically decodes all params. Now I got a javascript which does params="value="+encodeURIComponent('ab#cd'); and then calls http://server/controller?value=ab%23cd. If I access params[:value] in my controller, it contains ab%23cd and not ab#cd as I would expect.
How to solve this? Why does rails no auto decoding of this param?
Rails "automagically" handles parameters with the following logic.
If the request is GET it will decode anything in the query string:
GET http://server/controller?value=ab%23cd
On the server this will generate params['value'] as ab#cd
If the request is a POST with a query string it will not decode it:
POST http://server/controller?value=ab%23cd
On the server this will generate params['value'] as ab%23cd
If the request is a POST with data parameters, it will decode it:
POST http://server/controller
data: value=ab%23cd
On the server this will generate params['value'] as ab#cd
I suspect that you're seeing this issue because you're including a query string with a POST request instead of a GET request and so Rails isn't decoding the query string.

$_GET ajax request encoding problems

Am trying to send a variable from javascrpt client code, to php script at server side using ajax, the php scripts accepts a value from the script and makes query in database according to this value...the problem that am having an encoding issue, when i try to send a value with a special characters language (arabic) the query to the database fails in Google Chrome, and in Internet Expolrer, but it works fine in mozilla FF...is it common to have this encoding problem with ajax?? any sugestions?

Using JQuery AJAX with non-unicode sites

I'm using JQuery '.serialize' + '.post' to send form data using ajax to a non-unicode, asp based website. The result data is url-encoded in unicode (characters are encoded in double values).
Can I post the form data encoded like it's sent using the form submit(), using JQuery ajax?
Ok, after hours and hours of trying...
Javascript uses UTF-8 for strings, and AJAX requests are always sent with UTF-8, period. There will always be a conversion on the server side if UTF-8 is not supported.
Description in the JQuery contentType parameter states:
Data will always be transmitted to the server using UTF-8 charset; you
must decode this appropriately on the server side.
So, eventhough there are QA's which claim some solutions as working, they are simply some conversions on the client & the server side. So firefox's firebug will always show UTF-8 characters on the net panel for AJAX requests, and it will never look like a submitted form with single byte characters.
You can use
var encStr = encodeURIComponent(str)

Categories

Resources