2

I am passing data to my php script using jQuery's .ajax method. I am passing a very complex JSON object as data. At server side I receive data in $_POST variable all converted to php format.

How this conversion happen? Does it happen at the client side or the server side? Which modules associated in this process. Any source to understand complete process in depth.

Client request:

var data = { foo: 123, bar: 456, rows: [ { column1 : 'hello', column2 : 'hola', column3 : 'bonjour', }, { column1 : 'goodbye', column2 : 'hasta luego', column3 : 'au revoir', }, ], test1:{ test2: { test3: 'baz' } } }; $.ajax({ type: 'post', cache: false, url: './ajax/', data: data }); 

At Server Side My '$_POST' var:

Array ( [foo] => 123 [bar] => 456 [rows] => Array ( [0] => Array ( [column1] => hello [column2] => hola [column3] => bonjour ) [1] => Array ( [column1] => goodbye [column2] => hasta luego [column3] => au revoir ) ) [test1] => Array ( [test2] => Array ( [test3] => baz ) ) ) 

This code snippet is taken from here.

0

3 Answers 3

1

Jquery is converting the data into HTTP format. http://en.wikipedia.org/wiki/POST_%28HTTP%29

This link shows how arrays are encoded: http://php.net/manual/en/function.http-build-query.php

You can use PHP itself to generate the HTTP format. I converted your array to PHP format:

$data = array( 'foo' => 123, 'bar' => 456, 'rows' => array( 0 => array( 'column1' => 'hello', 'column2' => 'hola', 'column3' => 'bonjour'), 1 => array( 'column1' => 'hello', 'column2' => 'hola', 'column3' => 'bonjour')), 'test1' => array('test2' => array('test3' => 'baz')) ); 

Then you can generate the HTTP as follows:

echo http_build_query($data); 

I got the following result:

foo=123&bar=456&rows%5B0%5D%5Bcolumn1%5D=hello&rows%5B0%5D%5Bcolumn2%5D=hola&rows%5B0%5D%5Bcolumn3%5D=bonjour&rows%5B1%5D%5Bcolumn1%5D=hello&rows%5B1%5D%5Bcolumn2%5D=hola&rows%5B1%5D%5Bcolumn3%5D=bonjour&test1%5Btest2%5D%5Btest3%5D=baz 
Sign up to request clarification or add additional context in comments.

3 Comments

How nested data is specified? I tried reading about multipart/form-data but didn't able to understand how iti is done. Also then the conversion must happen in php from HTTP format to php format.
Yes, that's correct. PHP then converts the HTTP format into the its array format - GET, POST or REQUEST.
Give me a minute, and I'll try to help you more with that.
1

JSON is a universal data exchange format (to all languages that support its specification that is). The JSON data is encoded from a memory object to a JSON-formatted string by the language that is sending it, and decoded (from string to object) by the language that receives it.

An important point when talking about jQuery and JavaScript is that the syntax for a JSON looks similar to JavaScript, but it is actually more strict than the syntax for a regular JavaScript Object (see: What are the differences between JSON and JavaScript object?). For example, the JavaScript object literal you have posted above is not valid JSON, becuase both the keys and values are not surrounded by quotes. Additionally, there is technically no such thing as a JSON Object. A glob of JSON data is acutally just a String written in a subset of JavaScript's Object Notation.

So, PHP's json_encode($object) function and jQuery's encodeJSON([Object]) function will transform a memory object in their respective languages into a string that both languages (and others of course) can accept as data. The json_decode($string) and parseJSON([String]) functions in PHP and jQuery, respectively, take a JSON string and commit it to memory as an object.

4 Comments

But there is something more to this process, if we zoom in, as mentioned by @Pe de Leao.
Ah, what is this difference between HTTP format to php format?
@orb -- HTTP uses funny characters like: foo=bar&baz=boom&cow=milk&php=hypertext+processor
Oh I see it is the ACTUAL hypertext TRANSFER PROTOCOL side of it :-).
0

jQuery encodes the object data as a key value pairs, for example if we have:

var data = { foo: 123, bar: 456, rows: [ { column1 : 'hello', column2 : 'bonjour', }, { column1 : 'goodbye', column2 : 'au revoir', }, ] }; 

jquery will encode that object to the following string

foo=123&bar456&rows[][column1]=hello&rows[][column2]=bonjour&rows[][column1]=goodbye&rows[][column2]=au+revoir 

and PHP will convert that string in to an array and assign it to the $_GET or $_POST array depending of the request.

1 Comment

Also, url encoding is involved in the client side, I mean the key value pairs are url encoded before is send to the server

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.