How to use Ajax to receive JSON data in thinkphp, thinkphpjson_php tutorial

Source: Internet
Author: User

The method of using AJAX to receive JSON data in thinkphp, Thinkphpjson


The example in this article describes how to use Ajax to receive JSON data in thinkphp. Share to everyone for your reference. The specific analysis is as follows:

Here through the thinkphp+jquery implementation of Ajax, extended the next, wrote a query, the foreground code is as follows:

The

First need to introduce Jquery.js, the main code is as follows:
Copy code code as follows: function Ajax (id,pic) {
/// Since thinkphp does not parse the thinkphp constants in JavaScript, it needs to be defined here first.
var url= ' __url__ ';
$.ajax ({
url:url+ '/returnajax/id/' +id,//commits access to the URL
Type: ' GET ',//Method submitted
DataType: ' text ',//type of content returned, Since the PHP file is directly echo, this is the text
timeout:1000,//timeout
error:function () {///If an error occurs, execute function
Alert (' Error loading XML Document ');
},
Success:function (data) {
//alert (data);//If successful, pop-up information
writehtml (data,pic);
}
});
}
Function writehtml (data,pic) {
var product = eval (' (' + data + ') ');//Even if json.js is not introduced, it can be turned into a JSON object
//alert ($ ( "#cate_pic"). attr ("src"));
$ ("#cate_pic"). attr ("src", ".. /images/"+pic);
$ ("#product_pic"). attr ("src", ".. /attachments/product/"+product.attachpath+"/"+product.attachthumb";
$ ("#product_subject"). HTML (product.subject);
$ ("#product_content"). HTML (product.content);
}

Using the echo output in Product.class.php, the Json_encode () method in the thinkphp can automatically turn objects into JSON format
Copy the Code Code as follows: Public function Returnajax () {
$id = $_get[' id '];
$Product =d (' Product ')->where (' id= '. $id)->find ();
Returns a JSON-formatted dataset
echo Json_encode ($Product);
Print_r (Json_encode ($Product));
}

The returned data format is as follows:

Copy the Code code as follows:
{
"id": "9",
"userid": "1",
"CID": "10",
"CID": "10",
"Subject": "1111",
"Color": "",
"Spec": "",
"Size": "",
"keywords": "",
"Content": "

1111

",
"Meno": "1111",
"Attachpath": "200903",
"Attachment": "49d1d86e68d31.png",
"Attachthumb": "49d1d86e68d31_thumb.png"
}

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

http://www.bkjia.com/PHPjc/929673.html www.bkjia.com true http://www.bkjia.com/PHPjc/929673.html techarticle thinkphp the method of using AJAX to receive JSON data, Thinkphpjson This article describes the method of using AJAX to receive JSON data in thinkphp. Share to everyone for your reference. The specific analysis is as follows ...

  • 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.