C# 从jQuery调用处理程序不需要';行不通

C# 从jQuery调用处理程序不需要';行不通,c#,jquery,ajax,asp.net-3.5,handler,C#,Jquery,Ajax,Asp.net 3.5,Handler,我需要从jQuery调用一个处理程序(ashx)文件,以便在运行时获取一些数据。 我的jQuery函数如下所示: var pID = 3; var tID = 6; $("#Button1").click(function() { var urlToHandler = "Controller/TestHandler.ashx"; $.ajax({ type: "

我需要从jQuery调用一个处理程序(ashx)文件,以便在运行时获取一些数据。 我的jQuery函数如下所示:

         var pID = 3;
         var tID = 6;

         $("#Button1").click(function() {
            var urlToHandler = "Controller/TestHandler.ashx";
            $.ajax({
                type: "POST",
                url: urlToHandler,
                data: "{'pID':'" + pID + "', 'tID':'" + tID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    alert(msg);
                }
            });
        });
我的处理程序代码:

 <%@ WebHandler Language="C#" Class="TestHandler" %>

using System;
using System.Web;

public class TestHandler : IHttpHandler
{    
    public void ProcessRequest (HttpContext context)
    {
        String pID = context.Request.Params["pID"];
        String tID = context.Request.Params["tID"];
        context.Response.ContentType = "text/plain";
        context.Response.Write(pID + " " + tID);
    }

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

使用制度;
使用System.Web;
公共类TestHandler:IHttpHandler
{    
公共void ProcessRequest(HttpContext上下文)
{
字符串pID=context.Request.Params[“pID”];
字符串tID=context.Request.Params[“tID”];
context.Response.ContentType=“text/plain”;
context.Response.Write(pID+“”+tID);
}
公共布尔可重用
{
得到{
返回false;
}
}
}
问题是代码执行没有到达处理程序代码。 我可以从处理程序文件所在的同一目录中的同一jQuery函数调用其他web表单(aspx)文件。因此,这不是任何路径问题


我不熟悉这个处理程序文件的概念。我在谷歌上搜索了很多,但在我的代码中找不到任何错误

我认为您将json数据传递给处理程序的方式是不正确的

还要确保处理程序的路径正确,并在处理程序中向控制台写入一行代码,以检查它是否被调用。请尝试此代码

$("#Button1").click(function(){

        $.ajax({
            type: "POST",
            url: "/Controller/TestHandler.ashx",
            data: {pID:pID, tID:tID},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert(msg);
            },
            error: function(){
                alert("Error");
            }

        });
    });

我认为您将json数据传递给处理程序的方式是不正确的

还要确保处理程序的路径正确,并在处理程序中向控制台写入一行代码,以检查它是否被调用。请尝试此代码

$("#Button1").click(function(){

        $.ajax({
            type: "POST",
            url: "/Controller/TestHandler.ashx",
            data: {pID:pID, tID:tID},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert(msg);
            },
            error: function(){
                alert("Error");
            }

        });
    });

在改变了我传递json数据的方式(如@DRAKO所建议的)并从ajax回发调用中删除contentType之后,它就工作了。还修正了路径

$("#Button1").click(function() {
    var urlToHandler = "TestHandler.ashx";
    $.ajax({
        type: "POST",
        url: urlToHandler,
        data: { pID: pID, tID: tID },
        dataType: "json",
        success: function(msg) {
            //do something with the msg here...
        }
    });
});

在改变了我传递json数据的方式(如@DRAKO所建议的)并从ajax回发调用中删除contentType之后,它就工作了。还修正了路径

$("#Button1").click(function() {
    var urlToHandler = "TestHandler.ashx";
    $.ajax({
        type: "POST",
        url: urlToHandler,
        data: { pID: pID, tID: tID },
        dataType: "json",
        success: function(msg) {
            //do something with the msg here...
        }
    });
});

小提琴手怎么说?(或firebug?)那么,它返回200,成功?你是说“按钮1”而不是“按钮1”?按钮的ID是正确的。我已经发出警报并检查按钮点击事件是否触发。Fiddler怎么说?(或firebug?)那么,它返回200,成功?你是说“按钮1”而不是“按钮1”?按钮的ID是正确的。我已发出警报并检查按钮单击事件是否触发。路径正确,我可以从同一目录调用其他web表单(aspx)文件@DRAKO我正在尝试您建议的json数据更正。路径正确,我可以从同一目录调用其他web表单(aspx)文件@DRAKO我正在尝试您建议的json数据更正。