PHP代码
本地访问地址:http://192.168.1.168/index.php
输出结果:[{"my_id":"17"},{"my_id":"16"},{"my_id":"15"},{"my_id":"14"},{"my_id":"13"}]
<?php
//连接数据库省略
$query=mysql_query("select * from crj order by id desc limit 0,5");
while ($row=mysql_fetch_array($query)) {
$sayList[] = array(
"my_id" => $row['id'] ,
);
}
echo json_encode($sayList);
?>
JS代码
<script>
(function($) {
var methodEl = 'get';
var dataTypeEl = 'json';
var success = function(response) {
var dataType = dataTypeEl;
if (dataType === 'json') {
response = JSON.stringify(response);
} else if (dataType === 'xml') {
response = new XMLSerializer().serializeToString(response).replace(/</g, "<").replace(/>/g, ">");
}
alert(response);
};
var ajax = function() {
var url = "http://192.168.1.168/index.php";
var type = "get";
var dataType = "json";
var data = {};
if (type === 'get') {
if (dataType === 'json') {
$.getJSON(url, data, success);
} else {
$.get(url, data, success, dataType);
}
} else if (type === 'post') {
$.post(url, data, success, dataType);
}
};
ajax();
})(mui);
</script>
1 个回复
甜菜 (作者)
非常抱歉,一时大意,新建php文件用的HTML,把文件里的标签和头部声明去掉就可以乐!一_一!