Table of Contents
How to use ajax to receive json data in ThinkPHP, thinkphpjson
Home Backend Development PHP Tutorial How to use ajax to receive json data in ThinkPHP, thinkphpjson_PHP tutorial

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

Jul 13, 2016 am 10:11 AM
ajax json thinkphp

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

See all articles