本文主要是介绍ajax xmlhttprequest异步获取后台数据(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前台代码:
<%@ 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();
var sel1 = document.getElementById('cs1');
obj.open("get", "default.aspx?id=" + cs1.value, true); //记得小写,亲,并且default.aspx不区分大小写,这里在IE存在缓存问题,使用下面的可以解决
obj.open("get", "default.aspx?id=" + cs1.value + "&dt=" + new Date().getTime());//ajax异步在IE下面存在缓存,不会向后台请求
obj.send(); //记得小写,亲
obj.onreadystatechange = function () {//此处为回调函数
if (obj.readyState == 4 && obj.status == 200) {
alert(obj.responseText);
}
};
}
</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)
{
string id = Request.QueryString["id"];
Response.Write(id + "aa");
Response.End();
}
}
}
}
这篇关于ajax xmlhttprequest异步获取后台数据(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!