前台代码:
<%@ Page Title="主页" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ajax测试二._Default" %>
<html>
<head>
<script type="text/javascript">
function getHttpObj() {
var httpobj = null;
try {
httpobj = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpobj = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e1) {
httpobj = new XMLHttpRequest();
}
}
return httpobj;
}
function cs() {
var obj = getHttpObj();
obj.open("post","default.aspx?id=33",true);
obj.send();
obj.onreadystatechange = function () {//此处为回调函数
if (obj.readyState == 4 && obj.status == 200) {
alert(obj.responseText);//此处得到的是XML内容
alert(obj.responseXML.documentElement.getElementsByTagName('student')[0].text);//此处为处理方式,firefox,chrome为textContent,麻烦,哎
}
};
}
</script>
</head>
<body>
<select id="cs1" οnchange="cs();">
<option value="1">江西省</option>
<option value="2">福建省</option>
</select>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ajax测试二
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
Response.ContentType = "text/xml";//这个一定要有
Response.Charset = "UTF-8";//此处可以省略
Response.Write("<?xml version='1.0' encoding='gb2312' ?><Students><student id='99'>aa</student><cs kk='aa'>44</cs></Students>");//格式一定要符合XML,
//比如<?必须在一起,属性值要加上单引号或者双引号
Response.End();
}
}
}
}