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