刚学AJAX,问个简单的问题

百变猪娃娃 2012-04-22 01:54:30

<!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>
<title>无标题页</title>

<script src="js/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
function Button1_onclick(){
var amount = $("#Text1").val();
var moneytype = $("#Select1").val();

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //创建XMLHTTP对象
if(!xmlhttp){
alert("创建xmlhttp对象异常!");
return false;
}
xmlhttp.open("POST","HuiLv1.ashx?amount="+amount+"&moneytype="+moneytype+"&ts="+new Date(),false);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){ //如果状态吗是200则代表成功。
$("#Text2").val(xmlhttp.responseText);
}
else
{
alert("ajax服务器返回错误");
}
}
}
xmlhttp.send();
}
</script>
</head>
<body>
<input id="Text1" type="text" />
<select id="Select1" name="D1">
<option value="1">美元</option>
<option value="2">日元</option>
<option value="3">港币</option>
</select>
<input id="Button1" type="button" value="button" /><input id="Text2"
type="text" />
</body>
</html>

这是前台HTML页。
下面是后台ashx.cs

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace AJAX测试1
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class HuiLv1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string amount = context.Request["amount"];
int iAmount = Convert.ToInt32(amount);
string moneytype = context.Request["moneytype"];
if (moneytype == "1") //美元
{
context.Response.Write(iAmount / 7);
}
else if (moneytype == "2") //日元
{
context.Response.Write(iAmount * 10);
}
else if (moneytype == "3") //港币
{
context.Response.Write(iAmount * 10 / 9);
}
else
{

}
}

public bool IsReusable
{
get
{
return false;
}
}
}
}



为什么,我点击按钮,啥反应都没有呢?
...全文
55 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
百变猪娃娃 2012-04-22
  • 打赏
  • 举报
回复
我知道原因了。
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
上面这一句表示只能在IE中有效果。
我的默认浏览器是CHROME。。。。刚才换成IE就好了。
001007009 2012-04-22
  • 打赏
  • 举报
回复
楼主 用firebug 调试下,看是什么错误。
百变猪娃娃 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

没见你触发 Button1_onclick 啊


<input id="Button1" type="button" value="button" onclick="Button1_onclick()" />
[/Quote]
添加onclick后也没反应。
001007009 2012-04-22
  • 打赏
  • 举报
回复
没见你触发 Button1_onclick 啊


<input id="Button1" type="button" value="button" onclick="Button1_onclick()" />

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧