码迷,mamicode.com
首页 > Web开发 > 详细

Json在PHP与JS之间传输

时间:2015-08-01 23:27:01      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

1. JS-->PHP

a). JS create Json 

 

技术分享
 1 <script>
 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 </script>
Js code create json array object

 

b). Pass Json from JS to PHP by using Ajax

 

技术分享
 1 <script>
 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 </script>
JS side

 

技术分享
 1 <script>
 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 </script>
PHP side

 

2. PHP-->JS

a). PHP create Json

 

技术分享
 1 <?php
 2     
 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‘)?)

 

http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page

 

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

 

Json在PHP与JS之间传输

标签:

原文地址:http://www.cnblogs.com/bruceyo/p/4694489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!