String JSON object passing value

Source: Internet
Author: User

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

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.