Before the JSON, the use of Ajax, so some knowledge blind spots are so easily avoided. Recent projects need to use the table only son a JSON object, stepping on a small pit, strange only strange in their own reading when too not careful, the bottom to do a summary.
Demo1.html:
1 <Body>2 <formAction= "eg.php"Method= "POST"name= "FOM">3 <inputtype= "text"name= "txt" />4 <inputtype= "Submit" />5 </form>6 <Scriptsrc= "Jquery-1.11.3.min.js"></Script>7 <Scripttype= "Text/javascript">8 9 //creating a JSON objectTen var Person= { One "name": "Huyawei", A "Sex" : "male" - } - $("[Type=submit]"). Click (function(){ the //set the value of the text box to this JSON object - $("[Type=text]"). val (person); - }); - + </Script> - </Body>
Build a PHP file to receive the passed values
<?php var_dump (json_decode ($_post[' txt ")->name);? >
Submit Form
We will see such a result null;
Check carefully, the foreground of the JSON and arrays to the background, you need to convert to a string form. So, we just need to change the 16th line of the above HTML file to:
$ ("[Type=text]"). Val (json.stringify (person))
The JSON object is converted to a string form through the stringify function of the JSON object, so it's OK to look at the results in the browser.
Since the JSON is generally used before, Ajax will automatically convert your value into a string format, so you don't have to convert it yourself.
What do you understand not in place, but also hope you point out, thank you.
String JSON object passing value