function getUser() {
$.ajax({
type: "get",
url: "WelCome.aspx",
success: function (workId) {
if (workId != undefined && workId != null)
getUserInfo(workId)
else
$("#userInfo").text("获取域账号失败")
},
error: function (XMLResponse) { alertMsg(XMLResponse.responseText) }
})
}
<%
string name = User.Identity.Name;
Response.Write(name.Substring(name.LastIndexOf("\\") + 1));
%>
使用ajax获取aspx页面中的这个内容时,返回的一直是整个请求页面的html,然后加上datatype:'json',又走到error,并且返回的还是正确的值,是因为response.write返回的只是常规的字符串,并不是json对象,所以会error。
后来看到一篇博客,才真正解决了,就是在write之前,先清空掉response,然后再write就可以了
<%
string name = User.Identity.Name;
//在返回数据之前,清空response,不然ajax接收到的是整个html页面
Response.Clear();
Response.Write(name.Substring(name.LastIndexOf("\\") + 1));
Response.Flush();
Response.End();
%>