ThinkPHP中使用ajax接收json数据的方法_PHP
本文实例讲述了ThinkPHP中使用ajax接收json数据的方法。分享给大家供大家参考。具体分析如下:
这里通过ThinkPHP+jquery实现ajax,扩展了下,写了个查询,前台代码如下:
首先需要引入jquery.js,主要代码如下:
代码如下:
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);
}
Product.class.php中使用echo输出,thinkphp中json_encode()方法可将对象自动转成json格式
代码如下:
public function returnAjax(){
$id = $_GET['id'];
$Product=D('Product')->where('id='.$id)->find();
//返回一个json格式的数据集
echo json_encode($Product);
//print_r(json_encode($Product));
}
返回的数据格式如下:
代码如下:
{
"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"
}
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

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

AI Hentai Generator
Generate AI Hentai for free.

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.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

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.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

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.

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

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.
