100分求教 Ajax问题, htm 发送时 xmlHttp.status=404 “NotFound”

hinada99 2011-03-04 05:10:59

1>sendTest.htm中代码:
//点击按钮触发
function Goto()
{
sendReq("Middle.cs?info=hello",null,Receive);
}

//发送函数
function sendReq(url, data, stateChange){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("您的浏览器不支持AJAX");
return;
}
}
}
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
stateChange(xmlHttp.responseText);
}
else{
alert("服务端异常");
}
}
}
xmlHttp.open("post",url,true);
xmlHttp.send(data);
}

//接收函数
function Receive()
{
alert("Back Arrived");
}

2>web config中代码
<httpHandlers>
<add verb="*" path="Middle.cs" type="TESTWSL.Middle,TESTWSL"/>
</httpHandlers>

3>TESTWSL类库
Middle.cs代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using TESTBLL;

namespace TESTWSL
{
public class Middle:IHttpHandler
{
public bool IsReusable
{
// 如果无法为其他请求重用托管处理程序,则返回 false。
// 如果按请求保留某些状态信息,则通常这将为 true。
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
//在此写入您的处理程序实现。
if (context.Request.QueryString["info"].ToString() == "hello")
{
context.Response.Write(TESTBLL.Back.SendInfo());
}
}

}
}

4>TESTBLL类库
Back.cs代码
using System;
using System.Collections.Generic;
using System.Text;

namespace TESTBLL
{
public static class Back
{
public static string SendInfo()
{
return "You have arrived at backStage";
}
}
}



...全文
383 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Abafi 2011-03-08
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20110307/10/1af462ba-04fa-48b0-8da3-5c90345f2167.html
第72楼,我的标准答案,请楼主笑纳
荒牧 2011-03-08
  • 打赏
  • 举报
回复
~/所在文件夹路径/Middle.aspx?info=hello
荒牧 2011-03-08
  • 打赏
  • 举报
回复
应该是Middle.aspx?info=hello,你改过来试试
  • 打赏
  • 举报
回复
客户端是没办法直接访问.cs文件的,不管网站发布没有,.Net都会对项目进行编译(只不过没发布时是放在临时文件夹里了)
直接访问.aspx或者.ashx文件就行了,方法写在CodeBehind关联的cs文件里
hinada99 2011-03-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hb0513 的回复:]
.cs文件不行的 用.ashx这个文件试试
[/Quote]
context.Response.Write(TESTBLL.Back.SendInfo());
Response.End();

这段代码我写在了Middle.cs的
namespace TESTWSL
{
public class Middle : IHttpHandler, IRequiresSessionState
{
#region IHttpHandler Members
public bool IsReusable
{
// 如果无法为其他请求重用托管处理程序,则返回 false。
// 如果按请求保留某些状态信息,则通常这将为 true。
get { return true; }
}

public void ProcessRequest(HttpContext context)
{

context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
//在此写入您的处理程序实现。
if (context.Request.QueryString["info"].ToString() == "hello")
{
context.Response.Write(TESTBLL.Back.SendInfo());
}
}
#endregion

}
}
但问题是 前台的sendReq没能将请求发送过来。肯定是下面两处有问题吧

function Goto()
{
//htm 跳转到 htm并 传值
//window.showModalDialog("Get.htm",info,"dialogWidth=200px;dialogHeight=100px");
sendReq("Middle.aspx?info=hello",null,Receive);
//htm ajax 到前台

}

<httpHandlers>
<add verb="*" path="Middle.aspx" type="TESTWSL.Middle,TESTWSL"/>
</httpHandlers>
hinada99 2011-03-07
  • 打赏
  • 举报
回复
回12#,7#
请求的是.aspx,这个已经改了。
hinada99 2011-03-07
  • 打赏
  • 举报
回复

function Goto()
{
//htm 跳转到 htm并 传值
//window.showModalDialog("Get.htm",info,"dialogWidth=200px;dialogHeight=100px");
sendReq("Middle.aspx?info=hello",null,Receive);
//htm ajax 到前台

}

<httpHandlers>
<add verb="*" path="Middle.aspx" type="TESTWSL.Middle,TESTWSL"/>
</httpHandlers>
coley 2011-03-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 rock870210 的回复:]

<httpHandlers>
<add verb="*" path="Middle.cs" type="TESTWSL.Middle,TESTWSL"/>
</httpHandlers>
1、HttpHandler拦截的是页面,不是后台。这点你必须懂得。
2、Ajax请求的是页面,aspx,不是cs后台文件。这点你也必须懂得。
PS:如果非要使用Ajax连接cs文件,100分留……
[/Quote]
顶这个,观点一致
hinada99 2011-03-07
  • 打赏
  • 举报
回复
.cs文件行,继承了IHttpHandler接口就行。
3>TESTWSL类库
Middle.cs代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using TESTBLL;

namespace TESTWSL
{
public class Middle:IHttpHandler
{
public bool IsReusable
{
// 如果无法为其他请求重用托管处理程序,则返回 false。
// 如果按请求保留某些状态信息,则通常这将为 true。
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
//在此写入您的处理程序实现。
if (context.Request.QueryString["info"].ToString() == "hello")
{
context.Response.Write(TESTBLL.Back.SendInfo());
}
}

}
}
//是不是webconfig里面除了
<httpHandlers>
<add verb="*" path="Middle.cs" type="TESTWSL.Middle,TESTWSL"/>
</httpHandlers>
这段外,还要加其他代码?
hb0513 2011-03-05
  • 打赏
  • 举报
回复
.cs文件不行的 用.ashx这个文件试试
wuyq11 2011-03-04
  • 打赏
  • 举报
回复
context.Response.Write(TESTBLL.Back.SendInfo());
Response.End();

xmlHttp.open("POST",myurl,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = CallBack;
xmlHttp.send(MyWebSend);

function CallBack()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
var result=xmlHttp.responseText; }
}
}
tangtank 2011-03-04
  • 打赏
  • 举报
回复
404没找到对应响应页面,后边不用看了吧- -!菜鸟见解
Rock870210 2011-03-04
  • 打赏
  • 举报
回复
<httpHandlers>
<add verb="*" path="Middle.cs" type="TESTWSL.Middle,TESTWSL"/>
</httpHandlers>
1、HttpHandler拦截的是页面,不是后台。这点你必须懂得。
2、Ajax请求的是页面,aspx,不是cs后台文件。这点你也必须懂得。
PS:如果非要使用Ajax连接cs文件,100分留给楼主自己吧!

yan267 2011-03-04
  • 打赏
  • 举报
回复
function Goto()
{
sendReq("a.aspx?info=hello",null,Receive);
}


a.aspx

using TESTWSL;
using TESTBLL;


if (request.quest["info"]=="hello")
{

response.write(TESTBLL.Back.SendInfo())
}







hinada99 2011-03-04
  • 打赏
  • 举报
回复
达人快来帮忙看看
hinada99 2011-03-04
  • 打赏
  • 举报
回复
不能沉下去
hinada99 2011-03-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhouwei7682719 的回复:]
http://apps.hi.baidu.com/share/detail/17234544
[/Quote]

我想实现的 是网站 需要用 后台.NET 操作数据库。一定需要运行.cs文件里。
大波妹的的链接 尽管很好,但是只在 前台 js,htm中转。

上面代码怎么改能运行 到 WSL.Middle.cs中?
hinada99 2011-03-04
  • 打赏
  • 举报
回复
调试的 时候总是 无法 进行到 WSL中的Middle.cs中,

发送函数中,
if (xmlHttp.status == 200) {
stateChange(xmlHttp.responseText);
}
else{
alert("服务端异常");
}
这里报错,xmlHttp.status=404,"NotFound".
这个是因为webconfig的原因还是哪的原因。
求Ajax达人赐教, 使上述程序能够运行,或者提供一个 类似上面的 3层结构的 网站程序的达人,得100分~

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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