How does PHP return json data to jquery?

Source: Internet
Author: User
Json-format data is the data we always use in application development. json data is used when you submit data with jquery or when you submit data with APIs, so how does PHP return json data to jquery? I will introduce it to you later... json-format data is the data we always use in application development. json data is used when you submit data with jquery or when you submit data with APIs, so how does PHP return json data to jquery? I will introduce it to you.

To operate json data in jquery, we directly $. parseJSON (returnString ).

The instance code is as follows:

$(function () {$('#send').click(function () {$.getJSON('test.js', function (data) {$('#resText').emptyempty();var html = '';$.each(data, function (commentIndex, comment) {html += '

' + comment['username'] + ':

' + comment['content'] + '

';})$('#resText').html(html);})})})

All you need to do is store the data in the correct format. json or. js file. The following is the json format data transmitted in the example.

The instance code is as follows:

[{"Username": "Zhangsan", "content": "sofa. "},{" username ":" Li Si "," content ":" bench. "},{" username ":" Wang Wu "," content ":" floor. "}]

The json data mentioned above is fixed. how can we return json data using php?

Php output JSON format method

Add the header ('content-type: text/json') to the page. this header indicates that the output type of this file is json, what we see most in this form is the verification code-php output verification image. sometimes php can output css files and js files to do some interesting things. okay. let's test it.

The instance code is as follows:

  array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes"   => array("first", 5 => "second", "third") ); echo json_encode($fruits); ?>

The data read from the database is in json format.

The instance code is as follows:

   Tutorial provided by the first php network-generate the data read by the database in json format 
  Script

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.