我再做一个关于ajax的省市联动,ajax不怎么熟悉,出错了,都不知道怎么搞了,帮我看看。
<script type="text/javascript" language="javascript">
var XmlHttp;
function createXMLHttpRequest() {
//在IE下创建XMLHTTPRequest对象
try {
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
XmlHttp = null;
}
}
//在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象
if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
XmlHttp = new XMLHttpRequest();
}
return XmlHttp; }
function SendRequest(str) {
var url = "login.aspx?bk=" + escape(str);
createXMLHttpRequest();
XmlHttp.onreadystatechange = handleStateChange;
XmlHttp.open("GET", url, true);
XmlHttp.send(null);
}
function handleStateChange() {
if (XmlHttp.readyState == 4) {
if (XmlHttp.status == 200) {
var city_string = XmlHttp.responseText;
var city_array = city_string.split(",");
while (document.getElementById("city").options.length > 0) {
document.getElementById("city").options.remove(0);
}
for (var j = 0; j < city_array.length; j++) {
var oOption = document.createElement("OPTION"); //生成OPTION对象
oOption.text = city_array[j];
oOption.value = city_array[j];
document.getElementById("city").add(oOption); //把OPTION对象加入
}
}
else {
alert("请求页面异常!");
}
}
}
</script>
页面
<tr>
<td style="width: 92px"><span>所属省</span></td>
<td style="width: 154px"> <select id="province" onchange="SendRequest(this.value)" runat="server">
<option value="hn">河南省</option>
<option value="gd">广东省</option>
<option value="hb">湖北省</option>
<option value="js">江苏省</option>
</select>
</td>
</tr>
<tr>
<td style="width: 92px"><span>所属市:</span></td>
<td style="width: 154px"><select id="city" runat="server" style="width: 73px"> </select></td>
</tr>
后台:
protected void Page_Load(object sender, EventArgs e)
{
string bk, result = "";
bk = Request.QueryString["bk"].ToString().Trim();
if (bk != "")
{
switch (bk)
{
case "hn":
result = "郑州,洛阳,开封";
break;
case "gd":
result = "广州,深圳,珠海";
break;
case "hb":
result = "武汉,十堰,宜昌";
break;
case "js":
result = "南京,苏州,扬州";
break;
}
Response.Charset = "utf-8";
Response.Write(result);
}
}错误信息
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 20: {
行 21: string bk, result = "";
行 22: bk = Request.QueryString["bk"].ToString().Trim();
行 23: if (bk != "")
行 24: {
哎,这样怎么会出现这样的错误呢,ajax传值是这样的呀。
<script type="text/javascript" language="javascript">
var XmlHttp;
function createXMLHttpRequest() {
//在IE下创建XMLHTTPRequest对象
try {
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
XmlHttp = null;
}
}
//在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象
if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
XmlHttp = new XMLHttpRequest();
}
return XmlHttp; }
function SendRequest(str) {
var url = "login.aspx?bk=" + escape(str);
createXMLHttpRequest();
XmlHttp.onreadystatechange = handleStateChange;
XmlHttp.open("GET", url, true);
XmlHttp.send(null);
}
function handleStateChange() {
if (XmlHttp.readyState == 4) {
if (XmlHttp.status == 200) {
var city_string = XmlHttp.responseText;
var city_array = city_string.split(",");
while (document.getElementById("city").options.length > 0) {
document.getElementById("city").options.remove(0);
}
for (var j = 0; j < city_array.length; j++) {
var oOption = document.createElement("OPTION"); //生成OPTION对象
oOption.text = city_array[j];
oOption.value = city_array[j];
document.getElementById("city").add(oOption); //把OPTION对象加入
}
}
else {
alert("请求页面异常!");
}
}
}
</script>
页面
<tr>
<td style="width: 92px"><span>所属省</span></td>
<td style="width: 154px"> <select id="province" onchange="SendRequest(this.value)" runat="server">
<option value="hn">河南省</option>
<option value="gd">广东省</option>
<option value="hb">湖北省</option>
<option value="js">江苏省</option>
</select>
</td>
</tr>
<tr>
<td style="width: 92px"><span>所属市:</span></td>
<td style="width: 154px"><select id="city" runat="server" style="width: 73px"> </select></td>
</tr>
后台:
protected void Page_Load(object sender, EventArgs e)
{
string bk, result = "";
bk = Request.QueryString["bk"].ToString().Trim();
if (bk != "")
{
switch (bk)
{
case "hn":
result = "郑州,洛阳,开封";
break;
case "gd":
result = "广州,深圳,珠海";
break;
case "hb":
result = "武汉,十堰,宜昌";
break;
case "js":
result = "南京,苏州,扬州";
break;
}
Response.Charset = "utf-8";
Response.Write(result);
}
}错误信息
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 20: {
行 21: string bk, result = "";
行 22: bk = Request.QueryString["bk"].ToString().Trim();
行 23: if (bk != "")
行 24: {
哎,这样怎么会出现这样的错误呢,ajax传值是这样的呀。
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
行 23: if (bk != "")
行 24: {
=>bk = Request.QueryString["bk"]??"";if (bk != "")
{
switch (bk)
{
case "hn":
result = "郑州,洛阳,开封";
break;
case "gd":
result = "广州,深圳,珠海";
break;
case "hb":
result = "武汉,十堰,宜昌";
break;
case "js":
result = "南京,苏州,扬州";
break;
}
Response.Charset = "utf-8";
Response.Write(result);
}
createXMLHttpRequest();
XmlHttp.onreadystatechange = handleStateChange;
XmlHttp.open("GET", url, true);
XmlHttp.send(null);
get 方式的请求 记得加时间戳 or 随机数 避免缓存
{
string bk, result = "";
bk = Request.QueryString["bk"].ToString().Trim();
if (bk != "")
{
switch (bk)
{
case "hn":
result = "郑州,洛阳,开封";
break;
case "gd":
result = "广州,深圳,珠海";
break;
case "hb":
result = "武汉,十堰,宜昌";
break;
case "js":
result = "南京,苏州,扬州";
break;
}
Response.Charset = "utf-8";
Response.Flush();
Response.Write(result);
Response.End();
}
}
XmlHttp.send(null);在后面加上alert(url);看看url 是什么样的
context.Request.QueryString["bk"].ToString().Trim();
1,你的后台代码是login.aspx的后台代码吗?
2、你这个错是什么时候发生的?
<select id="province" onchange="SendRequest(this.value)" runat="server">
<option value="hn">河南省</option>
<option value="gd">广东省</option>
<option value="hb">湖北省</option>
<option value="js">江苏省</option>
</select>
或者
<select id="city" runat="server" />
是否在其他的容器控件里面?
<select id="province" onchange="SendRequest(this)" runat="server">
<option value="hn">河南省 </option>
<option value="gd">广东省 </option>
<option value="hb">湖北省 </option>
<option value="js">江苏省 </option>
</select>
然后SendRequest函数改改
function SendRequest(ddl) {
var url = "login.aspx?bk=" + escape(ddl.options[ddl.selectedIndex].value);
createXMLHttpRequest();
XmlHttp.onreadystatechange = handleStateChange;
XmlHttp.open("GET", url, true);
XmlHttp.send(null);
}
XmlHttp.send(null); alert(url);没有值??
是什么值, 我都下班了 也不知道 是什么值如果你仅仅只是 传递字母 可以不用编码var url = "login.aspx?bk=" + str +"&date="+new Date();因为是get 方式 要加上 时间戳 or 随机数相关文章 http://blog.csdn.net/Sandy945/archive/2009/05/12/4169870.aspx
public class RegistValidate : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
string name = context.Request.QueryString["username"].ToString());
System.Threading.Thread.Sleep(1000);
} public bool IsReusable
{
get
{
return false;
}
}
}
}一直没问题
非哥,这个bug弄的我太郁闷了,还是用数据表来实现比较简单,哎。
var url = "login.aspx?bk=" + escape(str);
alert(url);
createXMLHttpRequest();
XmlHttp.onreadystatechange = handleStateChange;
XmlHttp.open("GET", url, true);
XmlHttp.send(null);
}
-----------------------
看下alert 出来的是什么值
alert(url); 这看不到 alert 的值?
这个bk 先固定设一个值看能不能传过来.
var url = "login.aspx?bk=hn" 或者 你执行SendRequest 的时候alert(str)一下看看数据.
function SendRequest(str) {
var url = "Default2.aspx?bk=" + escape(str);
alert(url);
createXMLHttpRequest();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
我新开的一个页面。
Response.End(); 这个要加上的, 不过这个不关你这个问题.
用你的代码原封不动.我这里怎么测试都可以通过....
你在js 方法中都看不到alert 值 怎么能通
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript">
var XmlHttp;
function createXMLHttpRequest() {
//在IE下创建XMLHTTPRequest对象
try {
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
XmlHttp = null;
}
}
//在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象
if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
XmlHttp = new XMLHttpRequest();
}
return XmlHttp; }
function SendRequest(str) {
var url = "default2.aspx?bk=" + escape(str);
createXMLHttpRequest();
XmlHttp.onreadystatechange = handleStateChange;
XmlHttp.open("GET", url, true);
XmlHttp.send(null);
}
function handleStateChange() {
if (XmlHttp.readyState == 4) {
if (XmlHttp.status == 200) {
var city_string = XmlHttp.responseText;
var city_array = city_string.split(",");
while (document.getElementById("city").options.length > 0) {
document.getElementById("city").options.remove(0);
}
for (var j = 0; j < city_array.length; j++) {
var oOption = document.createElement("OPTION"); //生成OPTION对象
oOption.text = city_array[j];
oOption.value = city_array[j];
document.getElementById("city").add(oOption); //把OPTION对象加入
}
}
else {
alert("请求页面异常!");
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="province" onchange="SendRequest(this.value)" >
<option value="hn">河南省</option>
<option value="gd">广东省</option>
<option value="hb">湖北省</option>
<option value="js">江苏省</option></select>
<select id="city" style="width: 73px"> </select>
</div>
</form>
</body>
</html>
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
var XmlHttp;
function createXMLHttpRequest() {
//在IE下创建XMLHTTPRequest对象
try {
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
XmlHttp = null;
}
}
//在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象
if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
XmlHttp = new XMLHttpRequest();
}
return XmlHttp; }
function SendRequest(str) {
var url = "Default2.aspx?bk=" + escape(str);
alert(url);
createXMLHttpRequest();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
function handleStateChange() {
if (XmlHttp.readyState == 4) {
if (XmlHttp.status == 200) {
var city_string = XmlHttp.responseText;
var city_array = city_string.split(",");
while (document.getElementById("city").options.length > 0) {
document.getElementById("city").options.remove(0);
}
for (var j = 0; j < city_array.length; j++) {
var oOption = document.createElement("OPTION"); //生成OPTION对象
oOption.text = city_array[j];
oOption.value = city_array[j];
document.getElementById("city").add(oOption); //把OPTION对象加入
}
}
else {
alert("请求页面异常!");
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td style="width: 92px"><span>所属省</span></td>
<td style="width: 154px"> <select id="province" onchange="SendRequest(this.value)">
<option value="hn">河南省</option>
<option value="gd">广东省</option>
<option value="hb">湖北省</option>
<option value="js">江苏省</option>
</select>
</td>
</tr>
<tr>
<td style="width: 92px"><span>所属市:</span></td>
<td style="width: 154px"><select id="city" style="width: 73px"> </select></td>
</tr>
</table>
</form>
</body>
</html>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string bk, result = "";
bk = Request.QueryString["bk"].ToString().Trim();
if (bk != "")
{
switch (bk)
{
case "hn":
result = "郑州,洛阳,开封";
break;
case "gd":
result = "广州,深圳,珠海";
break;
case "hb":
result = "武汉,十堰,宜昌";
break;
case "js":
result = "南京,苏州,扬州";
break;
}
Response.Charset = "utf-8";
Response.Flush();
Response.Write(result);
Response.End();
}
}
}
这个???
{
if (!IsPostBack)
{
string bk, result = "";
bk = Request.QueryString["bk"].ToString().Trim();
if (bk != "")
{
switch (bk)
{
case "hn":
result = "郑州,洛阳,开封";
break;
case "gd":
result = "广州,深圳,珠海";
break;
case "hb":
result = "武汉,十堰,宜昌";
break;
case "js":
result = "南京,苏州,扬州";
break;
}
Response.Charset = "utf-8";
Response.Write(result);
Response.End();
} }
} 只是多了红色的. 但是用你的代码, 也正常通过.
bk = Request.QueryString["bk"].ToString().Trim();bk= Request.QueryString["bk"]??"";
bk = Request.QueryString["bk"].ToString().Trim();
这个写法应该属于编程习惯不好. Request.QueryString["bk"]有可能是null. 如果URL里面没有bk参数怎么? 如果URL里bk参数为零字节长怎么办? 你都没有考虑.
还是将程序写得健壮点吧.
<option value="new">请选择</option>
<option value="hn">河南省 </option>
<option value="gd">广东省 </option>
<option value="hb">湖北省 </option>
<option value="js">江苏省 </option> </select>
<select id="city" style="width: 73px"> </select>
还是为空
但是, 还是不知道你的为啥不能关联数据. 为你默哀.
SendRequest(this.value)可写成SendRequest(this.options[this.options.selectedIndex].value)
看看行不行. 估计与流览器版本有关. 看看你需要支持哪些流览器. 找到合适的写法.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title> <script type="text/javascript" language="javascript">
var XmlHttp;
function createXMLHttpRequest() {
//在IE下创建XMLHTTPRequest对象
try {
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
XmlHttp = null;
}
}
//在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象
if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
XmlHttp = new XMLHttpRequest();
}
return XmlHttp; }
function SendRequest(str) {
var url = "Temp.aspx?ajax=a&&bk=" + escape(str) + "&date=" + new Date();
if (str.length > 0) {
createXMLHttpRequest();
XmlHttp.onreadystatechange = handleStateChange;
XmlHttp.open("GET", url, true);
XmlHttp.send(null);
}
else {
document.getElementById("city").options.length = 0;
}
}
function handleStateChange() {
if (XmlHttp.readyState == 4) {
if (XmlHttp.status == 200) {
var city_string = XmlHttp.responseText;
var city_array = city_string.split(",");
while (document.getElementById("city").options.length > 0) {
document.getElementById("city").options.remove(0);
}
for (var j = 0; j < city_array.length; j++) {
var oOption = document.createElement("OPTION"); //生成OPTION对象
oOption.text = city_array[j];
oOption.value = city_array[j];
document.getElementById("city").add(oOption); //把OPTION对象加入
}
}
else {
alert("请求页面异常!");
}
}
}
</script></head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td style="width: 92px">
<span>所属省</span>
</td>
<td style="width: 154px">
<select id="province" onchange="SendRequest(this.value)" runat="server">
<option value="">--请选择--</option>
<option value="hn">河南省</option>
<option value="gd">广东省</option>
<option value="hb">湖北省</option>
<option value="js">江苏省</option>
</select>
</td>
</tr>
<tr>
<td style="width: 92px">
<span>所属市:</span>
</td>
<td style="width: 154px">
<select id="city" runat="server" style="width: 73px">
</select>
</td>
</tr>
</table>
</form>
</body>
</html>protected void Page_Load(object sender, EventArgs e)
{
string bk, result = "";
bk = Request.QueryString["bk"] ?? "";
if (Request.QueryString["ajax"] != null && bk != "")
{
switch (bk)
{
case "hn":
result = "郑州,洛阳,开封";
break;
case "gd":
result = "广州,深圳,珠海";
break;
case "hb":
result = "武汉,十堰,宜昌";
break;
case "js":
result = "南京,苏州,扬州";
break;
}
Response.Charset = "utf-8";
Response.Clear();
Response.Write(result);
Response.End();
} }