1. JS-->PHP
a). JS create Json
1
2 $(document).ready(function(){3 /*--JS create Json--*/
4 var jsonObject={}; //In another way: jsonObject={‘name‘:"Bruce",‘age‘:25};
5 jsonObject[‘name‘] = "Bruce";6 jsonObject[‘age‘] = 25;7 console.log(jsonObject);8 console.log(‘This is stringfied json object: ‘ +JSON.stringify(jsonObject));9 console.log(JSON.parse(JSON.stringify(jsonObject)));10 $("#demo").html(jsonObject.name + ", " +jsonObject.age);11 /*--JS create Json--*/
12
13 });14
Js code create json array object
b). Pass Json from JS to PHP by using Ajax
1
2 $(document).ready(function(){3 /*--JS create Json--*/
4 var jsonObject={}; //In another way: jsonObject={‘name‘:"Bruce",‘age‘:25};
5 jsonObject[‘name‘] = "Bruce";6 jsonObject[‘age‘] = 25;7 console.log(jsonObject);8 console.log(‘This is stringfied json object: ‘ +JSON.stringify(jsonObject));9 console.log(JSON.parse(JSON.stringify(jsonObject)));10 $("#demo").html(jsonObject.name + ", " +jsonObject.age);11 /*--JS create Json--*/
12
13 /*--Ajax pass data to php--*/
14 $.ajax({15 url: ‘php/test.php‘,16 type: ‘POST‘, //or use type: ‘GET‘, then use $_GET[‘json‘] or $_POST[‘json‘] to in PHP script
17 data: { json: JSON.stringify(jsonObject)},18 success: function(response) {19 console.log(response);20 var jsonObj =JSON.parse(response);21 $("#demo").html("From PHP‘s echo: " + jsonObj.name + ", " +jsonObj.age);22 }23 });24 /*--Ajax pass data to php--*/
25
26 });27
JS side
1
2 $(document).ready(function(){3 /*--JS create Json--*/
4 var jsonObject={}; //In another way: jsonObject={‘name‘:"Bruce",‘age‘:25};
5 jsonObject[‘name‘] = "Bruce";6 jsonObject[‘age‘] = 25;7 console.log(jsonObject);8 console.log(‘This is stringfied json object: ‘ + JSON.stringify(jsonObject));9 console.log(JSON.parse(JSON.stringify(jsonObject)));10 $("#demo").html(jsonObject.name + ", " +jsonObject.age);11 /*--JS create Json--*/
12
13 /*--Ajax pass data to php--*/
14 $.ajax({15 url: ‘php/test.php‘,
16 type: ‘POST‘, //or use type: ‘GET‘, then use $_GET[‘json‘] or $_POST[‘json‘] to in PHP script
17 data: { json: JSON.stringify(jsonObject)},
18 success: function(response) {19 console.log(response);20 var jsonObj = JSON.parse(response);21 $("#demo").html("From PHP‘s echo: " + jsonObj.name + ", " + jsonObj.age);22 }23 });24 /*--Ajax pass data to php--*/
25
26 });27
PHP side
2. PHP-->JS
a). PHP create Json
1 <?php2
3 $arr = array(4 ‘name‘ => "Bruce",
5 ‘age‘ => 25,
6 );7 echo json_encode($arr); //{"name":"Bruce","age":25}
8 echo $arr[‘name‘]; //Bruce
9 echo JSON_decode(json_encode($arr))->{‘name‘};//Bruce
10 echo implode((array)json_encode($arr)); //{"name":"Bruce","age":25}
11
12 ?>
PHP code
b). PHP cURL Call RESTful web service
coming soon
3. Pass Json from PHP to PHP (must be array then json_encode(‘json string‘)?)
4. Submit parameters to PHP through HTML form POST/GET to download a file (e.g. Excel...)
I figure out a way around this. Instead of making a POST call to force the browser to open the save dialog, I will make a POST call to generate the file, then temporary store the file on the server, return the filename . Then use a GET call for this file with "Content-Disposition: attachment; filename=filename1". The GET call with that header will force the browser to open the "Save this file" dialog, always.
//code: coming soon
原文:http://www.cnblogs.com/bruceyo/p/4694489.html