How to use ajax to receive json data in ThinkPHP
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); }
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)); }
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" }
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!

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
