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.