Home > Backend Development > PHP Tutorial > How to use ajax to receive json data in ThinkPHP

How to use ajax to receive json data in ThinkPHP

不言
Release: 2023-03-30 09:02:02
Original
2798 people have browsed it

This article mainly introduces the method of using ajax to receive json data in ThinkPHP, including the front-end js code and the corresponding PHP processing code, which is of great practical value. Friends who need it can refer to the example of 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:

Here, ajax is implemented through ThinkPHP jquery. It is expanded and a query is written. The front-end code is as follows:

First, jquery.js needs to be introduced. The main code is as follows:

Copy code The code is as follows:

function ajax(id,pic){
    //由于ThinkPHP不解析JavaScript里的ThinkPHP常量,所以需要先在这里定义。 
var URL='__URL__';
        $.ajax({
            url: URL+'/returnAjax/id/'+id,//提交访问的URL
            type: 'GET',//提交的方法
            dataType: 'text',//返回的内容的类型,由于PHP文件是直接echo的,那么这里就是text
            timeout: 1000,//超时时间
            error: function(){ //如果出错,执行函数
                alert('Error loading XML document');
            },
            success: function(data){
                //alert(data);//如果成功,弹出数据
                writeHtml(data,pic);
            }
        });
}
function writeHtml(data,pic){
    var product = eval('(' + data + ')'); //即使不引入json.js也可以转成json对象  
    //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);
}
Copy after login

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();  
        //返回一个json格式的数据集  
        echo json_encode($Product);  
//print_r(json_encode($Product));  
}
Copy after login

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":"<p>1111</p>",
    "meno":"1111",
    "attachpath":"200903",
    "attachment":"49d1d86e68d31.png",
    "attachthumb":"49d1d86e68d31_thumb.png"
}
Copy after login

Related recommendations:

ThinkPHP’s method of processing Ajax returns

The above is the detailed content of How to use ajax to receive json data in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

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