JSON is transferred between PHP and JS

Source: Internet
Author: User
Tags form post html form html form post php script

1. js-->php

a). JS Create Json

1<script>2$ (document). Ready (function(){3         /*--js Create json--*/4         varjsonobject={};//in another way:jsonobject={' name ': "Bruce", ' age ':--};5jsonobject[' name '] = "Bruce";6Jsonobject[' age '] = 25;7 Console.log (jsonobject);8Console.log (' This is stringfied JSON object: ' +json.stringify (Jsonobject));9 Console.log (Json.parse (json.stringify (Jsonobject)));Ten$ ("#demo"). HTML (jsonobject.name + "," +jsonobject.age); One         /*--js Create json--*/ A          -     }); -</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         varjsonobject={};//in another way:jsonobject={' name ': "Bruce", ' age ':--};5jsonobject[' name '] = "Bruce";6Jsonobject[' age '] = 25;7 Console.log (jsonobject);8Console.log (' This is stringfied JSON object: ' +json.stringify (Jsonobject));9 Console.log (Json.parse (json.stringify (Jsonobject)));Ten$ ("#demo"). HTML (jsonobject.name + "," +jsonobject.age); One         /*--js Create json--*/ A          -         /*--ajax Pass data to php--*/ - $.ajax ({ theURL: ' php/test.php ', -Type: ' POST ',//or use the type: ' GET ', then use $_get[' json '] or $_post[' json '] into PHP script - data: {json:JSON.stringify (Jsonobject)}, -Successfunction(response) { + Console.log (response); -                 varJsonobj =Json.parse (response); +$ ("#demo"). HTML ("from PHP ' s echo:" + Jsonobj.name + "," +jsonobj.age); A             } at         }); -         /*--ajax Pass data to php--*/ -          -     }); -</script>
JS Side

1<script>2$ (document). Ready (function(){3         /*--js Create json--*/4         varjsonobject={};//in another way:jsonobject={' name ': "Bruce", ' age ':--};5jsonobject[' name '] = "Bruce";6Jsonobject[' age '] = 25;7Console.Log(jsonobject);8Console.Log(' This is stringfied JSON object: ' + JSON.stringify (Jsonobject));9Console.Log(Json.parse (JSON.stringify (Jsonobject));Ten$ ("#demo"). HTML (jsonobject.name + "," +jsonobject.Age ); One         /*--js Create json--*/ A          -         /*--ajax Pass data to php--*/ -$.Ajax ({ theURL: ' php/test.php ', -Type: ' POST ',//or use the type: ' GET ', then use $_get[' json '] or $_post[' json '] into PHP script -Data: {json:JSON.stringify (Jsonobject)}, -Successfunction(response) { +Console.Log(response); -                 varJsonobj = JSON.Parse (response); +$ ("#demo"). HTML ("from PHP ' s echo:" + Jsonobj.name + "," + jsonobj.Age ); A             } at         }); -         /*--ajax Pass data to php--*/ -          -     }); -</script>
PHP Side

2. Php-->js

a). PHP Create Json

1<?PHP2     3     $arr=Array(4' Name ' = ' Bruce ',5' Age ' = 25,6     );7     EchoJson_encode ($arr);//{"name": "Bruce", "Age":8     Echo $arr[' Name '];//Bruce9     EchoJson_decode (Json_encode ($arr))->{' name '};//BruceTen     Echo implode((Array) Json_encode ($arr));//{"name": "Bruce", "Age": One      A?>
PHP Code

b). PHP CURL call RESTful Web Service

Coming soon

3. Pass Json from PHP to PHP (must is 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-a-around this. Instead of making a post call to force the browser to open the Save dialog, I'll 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 the header would force the browser-to-open the "Save this file" dialog, always.

Code: Coming Soon

JSON is transferred between PHP and JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.