jquery ajax从ashx处理程序获取返回值时出现问题

时间:2011-04-06 05:19:58

标签: jquery asp.net ajax ashx

问候,

无论我做什么,我都无法获取我的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

任何线索?

谢谢!

戴夫

3 个答案:

答案 0 :(得分:2)

尝试      url:“/ Handler1.ashx”,

而不是

 url: "http://localhost:49424/Handler1.ashx",

答案 1 :(得分: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"

来自我的处理程序,它增加了服务器端的工作,但是对服务器的调用也很好......它的返回给了我一些问题。

答案 2 :(得分: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);
}