1、前台js方法:
<script type="text/javascript">
function getMobile() {
//$("#Field_4").val()="";
var restMobile;
var iNumber = Math.random().toString();
var jsonUrl = "/getMobile.ashx?iNumber=" + iNumber + "&UserName="+$("#Field_2").val()+"&DeptName=" + $("#Field_3").val();
$.ajax({
type: "get",
async: false,
url: jsonUrl,
dataType: "json",
success: function (json) {
var obj = eval(json);
if (json.UserMobile.length==11) {
restMobile = json.UserMobile;
//alert('获取手机号码成功,为:\n\n' + restMobile);
} else {
restMobile = "";
//alert('获取手机号码失败,以下是错误提示:\n\n' +json.msg);
}
},
error: function () {
//alert('获取手机号码失败!');
}
});
$("#Field_4").val(restMobile);
}
if($("#Field_4").val()=="")
{getMobile();}
//$("#Field_4").dblclick(function(){getMobile();}); //按钮双击事件
</script>
2、ASP.NET后台代码方法(ashx文件):
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string UserName = context.Request.QueryString["UserName"];
string DeptName = context.Request.QueryString["DeptName"];
string strMsg = "", strStatus = "", strJSON = "", strMobile = "";
string strSQL = "select Mobile from Tab_UserInfo where RealName='" + UserName + "' ";
SvanSql zs = new SvanSql();
try
{
zs.Open(strSQL);
if (zs.rowcount > 0)
{
//strJSON = ConvertJson.ToJson(zs.m_table); //数组转换
strMobile = zs.m_table.Rows[0][0].ToString();
zs.Close();
strMsg = "读取数据成功";
strStatus = "1";
}
else
{
strMsg = "读取数据成功,但为0行;";
strStatus = "1";
strMobile = "手机为空";
}
}
catch (Exception ex)
{
strMsg = ex.Message;
strStatus = "0";
strMobile = "手机为空";
}
strJSON = "{\"status\":\"" + strStatus + "\",\"msg\":\"" + strMsg + "\",\"UserMobile\":\"" + strMobile + "\"}";
context.Response.Write(strJSON); //本域
context.Response.End();
}