


How to use ajax to receive json data in ThinkPHP, thinkphpjson_PHP tutorial
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:
//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
$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:
{
"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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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

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.

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.

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

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.
