Thinkphp method to receive JSON data using AJAX _php instance

Source: Internet
Author: User
Tags php template smarty template


The examples in this article describe the methods used to receive JSON data using Ajax 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:



First you need to introduce Jquery.js, the main code is as follows:

 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,//submit access URL
Type: ' Get ',//Submit method
DataType: ' text ',//Return of the type of content, because the PHP file is directly echo, then this is the text
timeout:1000,//Timeout time
Error:function () {//If error, execute function
Alert (' Error loading XML document ');
},
Success:function (data) {
alert (data);//If successful, eject
WriteHTML (Data,pic);
}
});
}
function writehtml (data,pic) {
var product = eval (' (' + data + ') '); Can be turned into a JSON object even without introducing json.js
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);
}





The echo output is used in Product.class.php, and the Json_encode () method in thinkphp can automatically convert an object to JSON format


Copy Code code as follows:
Public Function Returnajax () {
$id = $_get[' id '];
$Product =d (' Product ')->where (' id= '. $id)->find ();
Returns a data set in JSON format
echo Json_encode ($Product);
Print_r (Json_encode ($Product));
}





The data format returned is as follows:





Copy Code code as follows:

{
"id": "9",
"userid": "1",
"CID": "10",
"CID": "10",
"Subject": "1111",
"Color": "",
"Spec": "",
"Size": "",
"keywords": "",
"Content": "<p>1111</p>",
"Meno": "1111",
"Attachpath": "200903",
"Attachment": "49d1d86e68d31.png",
"Attachthumb": "49d1d86e68d31_thumb.png"
}





More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."



PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:



Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json



JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat



Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson



JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat



Online JSON compression/escape tool:



Http://tools.jb51.net/code/json_yasuo_trans



C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json



I hope this article will help you with the PHP program design based on thinkphp framework.


Related Article

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.