下面的代码写在ashx一般处理程序中
声明context.Response.ContentType = "application/json";代表服务器端返回的数据为json字符串
context.Response.Write(new JavaScriptSerializer().Serialize(new { status = "OK", Msg = "正确" }));
在html页面怎么接收里?(下面是没有用jQuery的ajax)
<script type="text/javascript">
window.onload = function () { document.getElementById("btn").onclick = function () {
alert("weqq");
var username = document.getElementById("uname").value;
var password = document.getElementById("pwd").value;
var xmh = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmh.open("post", "/Handler1.ashx?username=" + username + "&password=" + password, true);
xmh.onreadystatechange = function () {
if (xmh.readyState == 4) {
if (xmh.status == 200) {
//var x=eval('('+xmh.responseText+')');//把json字符串转换为json对象
//上下两种方法都行
var x = JSON.parse(xmh.responseText);//把json字符串转换为json对象
alert(x.status);
}
else {
alert("出错");
}
} };
xmh.send();
}
};
</script>