网上有很多解决这个问题的方法,试了一下都不好用,自己就对于这些方法测试了一下,然后逐个排除无用的设置,最后得到了最简单的方案。
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)js代码:
得到XmlHttpRequest的类
Code
1function HttpRequest()
2{
3 //取得Request对象
4 this.Request=function(){
5 try
6 {
7 if(window.XMLHttpRequest) request=new XMLHttpRequest();
8 if(!request)request=new ActiveXObject("Microsoft.XMLHTTP");
9 if(!request)request=new ActiveXObject("Msxml2.XMLHTTP");
10 return request;
11 }
12 catch(e)
13 {
14 alert("不支持XMLHTTPRequest");
15 }}
16}调用过程:
Code
1%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxTest._Default" %
2
3!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
4html xmlns="http://www.w3.org/1999/xhtml"
5head runat="server"
6 titleUntitled Page /title
7
8 script language="javascript" type="text/javascript" src="User.Base.js"/script
9
10 script language="javascript" type="text/javascript"
11 function Check()
12 {
13 var request=new HttpRequest().Request();
14 request.onreadystatechange=function (){
15 if(request.readyState==4)
16 {
17 if(request.status==200)
18 {
19 alert(request.responseText);
20 }
21 else
22 {
23 alert(request.responseText);
24 }
25 }
26 }
27 var value=document.getElementById("val").value;
28 request.open("POST","Default.aspx?Value="+value,true);
29 //设置防止乱码的方法,只要一句话就行
30 request.setRequestHeader("Content-Type","text/html;charset=gb2312");
31 request.send(null);
32 }
33 /script
34
35/head
36body
37 form id="form1" runat="server"
38 div
39 input id="val" type="text" onblur="Check()" /
40 /div
41 /form
42/body
43/html
44后台取得输入值:
protected void Page_Load(object sender, EventArgs e)
{
string value = Request["Value"];
}
测试过,可以得到正确的输入字符。