php 空格传输js,Json在PHP与JS之间传输

1. JS-->PHP

a). JS create Json

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

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

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

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

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

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

cdec0645add3fc3c328197dda5c76203.png

81178cc93a2a3bb5048d90d76e7ec935.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值