How to Use ajax to receive json data in ThinkPHP

Source: Internet
Author: User
This article describes how to use ajax to receive json data in ThinkPHP, including the front-end js Code and corresponding PHP processing code, which is very useful. For more information, see

This article describes how to use ajax to receive json data in ThinkPHP, including the front-end js Code and corresponding PHP processing code, which is very useful. For more information, see

This example describes how to use ajax to receive json data in ThinkPHP. Share it with you for your reference. The specific analysis is as follows:

Here, we use ThinkPHP + jquery to implement ajax. With the extension, we wrote a query. The front-end code is as follows:

First, introduce jquery. js. The main code is as follows:

The Code is as follows:

Function ajax (id, pic ){
// Because ThinkPHP does not parse the ThinkPHP constant in JavaScript, it must be defined here first.
Var URL = '/index. php/article ';
$. Ajax ({
Url: URL + '/returnAjax/id/' + id, // URL of the submitted access
Type: 'get', // method of submission
DataType: 'text', // type of the returned content. Because the PHP file is directly echo, text is used here.
Timeout: 1000, // timeout
Error: function () {// if an error occurs, execute the function
Alert ('error loading XML document ');
},
Success: function (data ){
// Alert (data); // If successful, the data is displayed.
WriteHtml (data, pic );
}
});
}
Function writeHtml (data, pic ){
Var product = eval ('+ data +'); // json objects can be converted even if json. js is not introduced.
// Alert ($ ("# cate_pic"). attr ("src "));
$ ("# Cate_pic"). attr ("src", "../images/" + pic );
$ ("# Product_pic"). attr ("src", ".../Attachments/product/" + product. attachpath + "/" + product. attachthumb );
$ ("# Product_subject" example .html (product. subject );
$ ("# Product_content" pai.html (product. content );
}

Echo output is used in Product. class. php. The json_encode () method in thinkphp can automatically convert the object to json format.

The Code is as follows:

Public function returnAjax (){
$ Id = $ _ GET ['id'];
$ Product = D ('product')-> where ('Id = '. $ id)-> find ();
// Return a data set in json format
Echo json_encode ($ Product );
// Print_r (json_encode ($ Product ));
}

The returned data format is as follows:

The Code is 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"
}

I hope this article will help you design PHP programs based on the ThinkPHP framework.

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.