$("document").ready(function () {
var url = "server.php";
var data = {'inAjax': 1, 'do': 'checkMember', 'username': 'demo'};
$.ajax({
url: url,
type: 'post',
data: data,
dataType: 'json',
success: function (res) {
//把json字符串转换为js对象,用来调用数据
// var jsonObj = JSON.parse(res);
$("#username").val(res.username);
$("#groupname").val(res.groupname);
$("#uid").val(res.uid);
},
error: function () {
alert('请求异常');
}
});
});
这样写,就可以不用转换了,都约定为json,直接调用就好<?php
error_reporting(0);
$inAjax = $_POST['inAjax'];
$do = $_POST['do'];
$do = $do ? $do : "default";
if (!$inAjax)
return false;
include_once "db.class.php";
switch ($do) {
case "checkMember":
$username = $_POST['username'];
$sql = "SELECT * FROM check_member WHERE username='$username'";
$result = $dbObj->getOne($sql);
$resJson = json_encode($result);
echo (!empty($result)) ? $resJson: "null";
break;
case "default":
die("nothing");
break;
}