Saeid
Saeid

Reputation: 13582

How run jQuery Use HttpHandler

This is My Code 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>

This is 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>

And Handler:

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");
        }
    }
}

I can't config IIS 7.0 Correctly as 15Seconds said, just could add my custom extention(.assq) like this:

enter image description here

When load the page and change Select the script don't work enter image description here

And this is My Rsponse: Unknown web method MyMethod.
Parameter name: methodName body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 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- size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }

        <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)
-->

why this code not work? Why the response say: Unknown web method MyMethod.? is this because of config IIS 7.0 or other thing?

KBoek help to find what's the problem: main one is: this 3 element cause confused jQuery:

data: "{}", contentType: "application/json; charset=utf-8", dataType: "json",

and I remove it.

Upvotes: 2

Views: 1480

Answers (3)

KBoek
KBoek

Reputation: 5975

I'd rather use the following setup:

Call the following URL from JavaScript:

"http://localhost/Sample001/MyMethod.assq"
Then, register this URL in web.config
<add name="MyMethod" verb="*" path="MyMethod.assq" type="Sample001.MyHandler,Sample001" preCondition="integratedMode" />
You don't have to register *.assq as a MIME type in IIS.

By the way, neither your JavaScript nor your C# code present any data in JSON format. You don't need that if you just want to return a string from the server. But of course, JSON is a good way to return a complete object to the client.

Alternatives to identify "lbl1" from Javascript

<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>

Upvotes: 1

VinayC
VinayC

Reputation: 49195

The issue is that in java-script code, you are not requesting your http handler but rather calling Default.aspx. Check the below line:

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

change it (so that your handler gets invoked) - for example,

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

Based on your configuration, you can use any file name with extension assq and that should do the trick. The path info ('/MyMethod') is also not needed.

On side note, you don't have to fiddle with IIS MIME Types - it is applicable only for static files.

Further, the original url syntax that you had used is typically used for invoking page methods - see this article if that's what you want to achieve.

Upvotes: 0

rick schott
rick schott

Reputation: 21117

$(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;
                }
            });
        });
    });

Upvotes: 0

Related Questions