JS—我的第六个AJAX程序(前端从数据库中获取数据库中数据,JSON传输)
来源:互联网 发布:dnf优化后盒子无法换装 编辑:程序博客网 时间:2024/06/07 08:03
前端:
common.js
function $(id) {return document.getElementById(id);}function createXhr(){var xhr;if(window.XMLHttpRequest){xhr = new XMLHttpRequest();}else{xhr = new ActiveXObject("Microsoft XMLHttp");}return xhr;}index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><script src="common.js"></script></head><body><button onclick="getMsg()">显示数据</button><script>function getMsg(){var xhr = createXhr();xhr.open("get","users.php",true);xhr.onreadystatechange = function() {if(xhr.readyState == 4 && xhr.status == 200){var resText = xhr.responseText;var arr = JSON.parse(resText);for(var i in arr){console.log("编号:"+arr[i].uid);console.log("用户姓名:"+arr[i].uname);console.log("电子邮件:"+arr[i].email);console.log("联系方式:"+arr[i].phone);}}}xhr.send(null);}</script></body></html>后台:
init.php
<?php $conn = mysqli_connect("127.0.0.1","root","","xz",3306);$initsql = "SET NAMES UTF8";mysqli_query($conn,$initsql);?>users.php
<?php header("Content-Type:application/json");require("init.php");$sql = "SELECT * FROM xz_user";$result = mysqli_query($conn,$sql);if($result === false){echo null;}else{$count = mysqli_affected_rows($conn);if($count == 0){echo 0;}else{$arr = mysqli_fetch_all($result,1);$arr = json_encode($arr);echo $arr;}}?>
阅读全文
0 0
- JS—我的第六个AJAX程序(前端从数据库中获取数据库中数据,JSON传输)
- json从数据库中获取数据
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- 从数据库获取到json数据,前端用vue.js数据绑定
- 在ofbiz中从多个数据库获取数据
- ajax从数据库获取数据的代码
- 从数据库中获取数据并输出
- 从数据库中获取第一条数据
- jquery从数据库中获取数据装换成json数据实现三级联动
- PHP中,Json AJax传输数据
- android app如何从数据库中获取需要的数据?
- zabbix 监控统计从数据库中获取想要的数据。
- 2017精华版:jquery 中 ajax,后台获取数据库json数据,显示在html页面 02
- JS——我的第三个AJAX程序(带数据库的用户名输入验证,模仿注册)
- 从数据库中获取单个数据——ExecuteScalar用法
- JS通过ajax方式从数据库动态获取数据的代码
- Android科大讯飞语音集成,非常详细的使用讲解
- 适配器模式(C++)
- 完数
- 这几天的socket编程
- iOS程序执行顺序和UIViewController 的生命周期(整理)
- JS—我的第六个AJAX程序(前端从数据库中获取数据库中数据,JSON传输)
- windows下安装scrapy问题解决办法
- Java类和对象 详解
- 网上购物车程序
- How do CUDA blocks/warps/threads map onto CUDA cores?
- Linux中vim的命令备忘录
- F
- 穷举n位二进制
- HDU2045 LELE的RPG难题