Encodeuricomponent decoding it in rails - javascript

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.

Related

Is JSON not serialise-able over http or Django doesn't know how to read it?

I have created a couple of projects with Django.
Every time I try to send an ajax POST request from frontend to django, I have to use JSON.stringify() function, pass it inside the body of POST request and parse the JSON manually inside Django (using ujson, because of its faster).
My question is that why do I need to do this manually?

Encoding URL into ASCII using 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?

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?
;)

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.

Can a yui aSync Request send objects as parameters?

Is there a way that you can pass in a javascript object to a yui aSync request or does it only accept a URI?
A YUI2 AsyncRequest sends a HTTP request to a URL using the request type you specify.
You are free to append information as query parameters for a GET or POST, or to send it as POST data.
To do so you could write a simple for in loop to iterate over your object & create a query string that you can then either set as the POST data or append to your URL if sending a GET. Be sure to use encodeURIComponent on the components as you are building your string.

Categories

Resources