How to use ajax to receive json data in ThinkPHP, thinkphpjson_PHP tutorial

WBOY
Release: 2016-07-13 10:11:24
Original
1368 people have browsed it

How to use ajax to receive json data in ThinkPHP, thinkphpjson

The example in this article describes the method of using ajax to receive json data in ThinkPHP. Share it with everyone for your reference. The specific analysis is as follows:

Ajax is implemented here through ThinkPHP+jquery. It is expanded and a query is written. The front-end code is as follows:

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

Copy code The code is as follows:
function ajax(id,pic){
//Since ThinkPHP does not parse ThinkPHP constants in JavaScript, they need to be defined here first.
var URL='__URL__';
          $.ajax({
​​​​​​ url: URL+'/returnAjax/id/'+id,//Submit the URL for access
            type: 'GET', // Submission method
                  dataType: 'text', //The type of content returned, since the PHP file is echoed directly, then here is text
            timeout: 1000, // timeout time
​​​​​​ error: function(){ //If an error occurs, execute the function
alert('Error loading XML document');
            },
Success: function(data){
                       //alert(data);//If successful, pop up the data
                    writeHtml(data,pic);
            }
        });
}
function writeHtml(data,pic){
var product = eval('(' + data + ')'); //It can be converted 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);
}

Use echo output in Product.class.php. The json_encode() method in thinkphp can automatically convert the object into json format

Copy code 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:

Copy code 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 be helpful to everyone’s PHP programming based on the ThinkPHP framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/929673.htmlTechArticleHow to use ajax to receive json data in ThinkPHP, thinkphpjson This article describes the method of using ajax to receive json data in ThinkPHP . Share it with everyone for your reference. The specific analysis is as follows...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!