gpt4 book ai didi

c# - 如何运行 jQuery 使用 HttpHandler

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:41 24 4
gpt4 key购买 nike

这是我的代码 Default.aspx:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Sample001.Default" %>
<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="lbl1" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="masterlbl" Text="Master" runat="server" />
</td>
<td>
<span class="Mastercs">
<asp:DropDownList ID="ddl1" runat="server"/>
</span>
</td>
<td>
<asp:Label ID="slavelbl" Text="Slave" runat="server" />
</td>
<td>
<span class="slavecs">
<asp:DropDownList ID="ddl2" runat="server" />
</span>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$('span.Mastercs select').change(function () {
$.ajax({
type: "POST",
url: "http://localhost/Sample001/Default.aspx/MyMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$('#lbl1').text = msg;
}
});
});
});
</script>
</body>
</html>

这是 Web.Config:

<?xml version="1.0"?>
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="MyMethod" verb="*" path="*.assq"
type="Sample001.MyHandler,Sample001" preCondition="integratedMode" />
</handlers>
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>

和处理程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Sample001 {
public class MyHandler : IHttpHandler {
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
context.Response.Write("Yeeeeeeeeeeeeeee Like it");
}
}
}

我无法将 IIS 7.0 正确配置为 15Seconds说,只是可以像这样添加我的自定义扩展名(.assq):

enter image description here

加载页面并更改选择脚本时不起作用 enter image description here

这是我的回复: 未知的 Web 方法 MyMethod。
参数名称:methodName 正文{字体系列:“Verdana”;字体粗细:正常;字体大小: .7em;颜色:黑色;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font- 尺寸:14pt;颜色:栗色 } pre {font-family:"Lucida Console";font-size: .9em} .marker {字体粗细:粗体;颜色:黑色;文字装饰:无;} .version {颜色:灰色;} .error {margin-bottom: 10px;} .expandable { 文本装饰:下划线;字体粗细:粗体;颜色:海军蓝; 光标:手; }

        <span><H1>Server Error in '/Sample001' Application.<hr width=100% size=1 
color=silver></H1>

<h2> <i>Unknown web method MyMethod.<br>Parameter name: methodName</i>
</h2></span>

<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

<b> Description: </b>An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more information about the
error and where it originated in the code.

<br><br>

<b> Exception Details: </b>System.ArgumentException: Unknown web method
MyMethod.<br>Parameter name: methodName<br><br>

<b>Source Error:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>

An unhandled exception was generated during the execution of the current web request.
Information regarding the origin and location of the exception can be identified using
the exception stack trace below.</code>

</td>
</tr>
</table>

<br>

<b>Stack Trace:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>

[ArgumentException: Unknown web method MyMethod.
Parameter name: methodName]
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs
eventArgs) +897827
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+80
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp;
completedSynchronously) +270
</pre></code>

</td>
</tr>
</table>

<br>

<hr width=100% size=1 color=silver>

<b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.237

</font>

</body>
</html>
<!--
[ArgumentException]: Unknown web method MyMethod.
Parameter name: methodName
at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender,
EventArgs eventArgs)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep
.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)
-->

为什么这段代码不起作用?为什么响应说:Unknown web method MyMethod.?这是因为配置 IIS 7.0 还是其他原因?

KBoek 帮助查找问题所在:主要的一个是:这 3 个元素导致 jQuery 混淆:

数据:“{}”,contentType: "application/json; charset=utf-8",数据类型:“json”,

然后我将其删除。

最佳答案

我宁愿使用以下设置:

从 JavaScript 调用以下 URL:

"http://localhost/Sample001/MyMethod.assq"
然后,在 web.config 中注册此 URL
<add name="MyMethod" verb="*" path="MyMethod.assq" type="Sample001.MyHandler,Sample001" preCondition="integratedMode" />
您不必在 IIS 中将 *.assq 注册为 MIME 类型。

顺便说一下,您的 JavaScript 和 C# 代码都没有以 JSON 格式呈现任何数据。如果您只想从服务器返回一个字符串,则不需要它。但当然,JSON 是向客户端返回完整对象的好方法。

从 Javascript 识别“lbl1”的替代方法

<asp:Label ID="lbl1" runat="server" CssClass="myLabel" />

<script type="text/javascript">
var label = $('.myLabel');
</script>
<span ID="lbl1">

<script type="text/javascript">
var label = $('#lbl1');
</script>

关于c# - 如何运行 jQuery 使用 HttpHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7966349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com