【问题标题】:How run jQuery Use HttpHandler如何运行 jQuery 使用 HttpHandler
【发布时间】:2011-12-19 11:06:28
【问题描述】:

这是我的代码 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");
        }
    }
}

我无法像15Seconds 所说的那样正确配置 IIS 7.0,只能像这样添加我的自定义扩展 (.assq):

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

这是我的回复: 未知的 Web 方法 MyMethod.
参数名称:methodName 正文 {字体系列:“Verdana”;字体重量:正常;字体大小: .7em;颜色:黑色;} p {字体系列:“Verdana”;字体重量:正常;颜色:黑色;边距顶部:-5px} b {字体系列:“Verdana”;字体粗细:粗体;颜色:黑色;上边距:-5px} H1 {字体系列:“Verdana”;字体重量:正常;字体大小:18pt;颜色:红色} H2 {字体系列:“Verdana”;字体重量:正常;字体- 尺寸:14pt;颜色:栗色 } 前 {字体系列:“Lucida 控制台”;字体大小:.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: "应用程序/json; charset=utf-8", 数据类型:“json”,

然后我删除它。

【问题讨论】:

  • 你上传的图片坏了。除此之外,如果您能提供实际的错误消息,那将会很有用。

标签: c# jquery asp.net httphandler


【解决方案1】:

我宁愿使用以下设置:

从 JavaScript 调用以下 URL:

"http://localhost/Sample001/MyMethod.assq"
然后,在web.config中注册这个URL
&lt;add name="MyMethod" verb="*" path="MyMethod.assq" type="Sample001.MyHandler,Sample001" preCondition="integratedMode" /&gt;
您不必在 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>

【讨论】:

  • 谢谢 KBoek,响应正确传递(在 Firebug 控制台中查看)但标签 (lbl1) 没有显示任何内容。
  • 那是因为“lbl1”是一个ASP.NET标签,在解析html时会得到不同的ID。您可以通过使用 Firebug 检查找到确切的 ID,但最好使用跨度而不是 asp:Label,或者使用不同的方式在 JavaScript 中识别标签 - 例如。给它一个唯一的类名并使用 $('.uniqueClassName') 作为选择器。
  • 你的权利,asp:Label 渲染与该 id 跨越,但我不能改变它,我也定义了一个标签而不是 asp:label 但我也不能改变它的文本.
  • 也在firebug中用console.log测试它,脚本正确识别span但不能修改文本
  • 那是因为你使用了错误的 jQuery 语法。这不是 $('#lbl1').text = msg;但是 $('#lbl1').html(msg);
【解决方案2】:

问题在于,在 java 脚本代码中,您不是在请求您的 http 处理程序,而是在调用 Default.aspx。检查以下行:

url: "http://localhost/Sample001/Default.aspx/MyMethod",

更改它(以便调用您的处理程序) - 例如,

url: "http://localhost/Sample001/xyz.assq/MyMethod",

根据您的配置,您可以使用任何扩展名为 assq 的文件名,这样就可以解决问题。也不需要路径信息('/MyMethod')。

附带说明,您不必摆弄 IIS MIME 类型 - 它仅适用于静态文件。

此外,您使用的原始 url 语法通常用于调用页面方法 - 如果您想要实现,请参阅 this article

【讨论】:

    【解决方案3】:
    $(document).ready(function () {
            $('span.Mastercs select').change(function () {
                $.ajax({
                    type: "POST",
                    url: "http://localhost/Sample001/MyMethod.assq",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        $('#lbl1').text = msg;
                    }
                });
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 2018-11-27
      • 2012-01-25
      相关资源
      最近更新 更多