Can a yui aSync Request send objects as parameters? - javascript

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.

Related

Is it safe to make a POST request with JSON data using ajax?

I'm working on web application where I need to send some data using ajax with post method. So I have two choices to send data whether in JSON format or query prams. I'm confused which should I use? and is it safe to send data in JSON format?
As #lucasreta mentioned, if you use HTTPS, it doesn't really matter either way.
Both methods are widely used. I know that Google accepts a Post request with query params and responds with a JSON object for ReCaptcha Server side validation.
Sometimes, the decision to use one or the other (or both) is dependent on how easy your chosen back-end technology makes it for you to either parse out query params or serialize JSON.
I will say that there is a general trend in using JSON in the request body as opposed to query params.
I found a couple of SO questions that are more down the lines of what you are asking...
REST API Best practices: args in query string vs in request body
REST API Best practices: Where to put parameters?

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?

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.

using backbone to put data into rest server

I have a question.
If i have a model named Input. It contains model Invoice and Collection InvoiceDetailCollection.
I modified the backbone.js specifically create:"POST" into create:"PUT" inorder to allow PUT since my service doesn't use POST.
If i were to use Input.save() What should my server expect as a request? I mean already have set a service method to expect string since if i expect string i can't use the input.save();
What should be the right parameter i should expect on my server side if i were to use the Input.save() method of backbone.js
By default, Backbone sends application/json data and the server should expects JSON data. You'll want to decode it. I use PHP and middleware that automatically json_decode($data) and turns it into an associative array for me to manipulate.
If this isn't possible, (can't take JSON data) I think what you want is to use emulateJSON It will serialize your data and send it as application/x-www-form-urlencoded like an HTML form.
http://documentcloud.github.com/backbone/#Sync-emulateJSON

How to get ajax querystring from JavaScript given data object?

When you make an AJAX request using jQuery, you can specify the data property that will be passed to the server. If the data contains an array of objects it passes it to the server in a format something like this:
MyArray[0][MyPropertyA]=123&MyArray[1][MyPropertyA]=456
Is there a way to get the query string it will generate for the AJAX request without actually doing the AJAX request?
jQuery param is your friend.
From the jQuery Param page:
Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.

Categories

Resources