jQuery Ajax在从ASHX处理程序获取返回值时存在问题

7

你好,

无论我做什么,我的jquery ajax代码都不能从ashx处理程序页面获取除null以外的响应。

这是我的hmt页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>:: ashx tester ::</title>
    <link rel="stylesheet" href="js/jqueryui/1.8.6/themes/sunny/jquery-ui.css"
        type="text/css" media="all" />
    <script type="text/javascript" src="js/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript" src="js/jqueryui/1.8.6/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/json2/0.0.0/json2.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#btnSubmit').click(
                function () {
                    DoIt();
                }
            );
        });

        function DoIt() {
            $('#btnSubmit').hide();
            $.ajax({
                type: "POST",                
                url: "http://localhost:49424/Handler1.ashx",
                data: {
                    firstName: 'Bob',
                    lastName: 'Mahoney'
                },
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert('success: ' + response);
                    $('#btnSubmit').show();
                },
                error: function (response) {
                    alert('error: ' + response);
                    $('#btnSubmit').show();
                }
            });
        }
    </script>
</head>
<body>
    <input id="btnSubmit" type="button" value="Submit" />
</body>
</html>

这是我的ashx页面:

Imports System.Web
Imports System.Web.Services
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data
Imports System.Configuration.ConfigurationManager
Imports System.Web.Services.Protocols
Imports System.Web.Script.Serialization

Public Class Handler1
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim j As New System.Web.Script.Serialization.JavaScriptSerializer
        Dim s As String

        s = j.Serialize(Now)
        context.Response.ContentType = "application/json"
        context.Response.Write(s)

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

有什么线索吗?

谢谢!

Dave

3个回答

2
尝试使用url: "/Handler1.ashx",而不是
 url: "http://localhost:49424/Handler1.ashx",

好的...这里有个奇怪的部分。那个方法虽然可行,但并不完美。我需要页面能够远程调用我的ashx处理程序。但是为了测试,我将页面复制到了我的项目中,并进行了您建议的更改。它起作用了!然后我再次尝试在IE中使用远程页面...我一直在使用chrome测试远程页面。结果发现,远程页面的ajax调用在IE中可以工作,但在chrome或firefox中无法工作! - Dave

1

我只使用带有处理程序的文本数据类型

      var webServiceURL = 'http://localhost:12400/Handler1.ashx';
        var data = "Key:Value";

        alert(data);

        $("input[id='btnSubmitLead']").click(function () {
            $.ajax({
                type: "POST",
                url: webServiceURL,
                data: data,
                dataType: "text",
                success: function (results) {
                    alert("Your information has been sent to the dealer, thank you");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus +"-"+ errorThrown);
                    alert("Your information has not been sent");
                }
            });
        });

所以,在所有浏览器中,我都能够将信息传递给处理程序,但是我没有得到正确的确认返回,因为它总是一个错误。我使用一个html页面提交ajax调用。我还删除了:

         context.Response.ContentType = "application/json"

我的处理程序增加了服务器端的工作量,但是对服务器的调用也很好...问题出在返回结果上。


0

这个简化的代码对我来说可行..

    $.ajax({
        type: "POST",
        url: "AddProviderToCall.ashx",
        data: {ProviderID: strProviderID, PhyUserID: PhyUserID },
        success: function (response) {
            alert('success: ' + response);
        },
    });

在我的 C# 处理程序中...
{
    context.Response.ContentType = "text/plain";
    context.Response.Write(result);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接