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

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

有什么线索吗?

谢谢!

戴夫

最佳答案

尝试一下 url: "/Handler1.ashx",

而不是

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

关于jquery ajax 从 ashx 处理程序获取返回值时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5561791/

相关文章:

c# - 用于指定 .net 框架版本的 C# 编译器选项是什么

jquery - 使用 jQuery 从 json 中仅获取一些元素

javascript - JQuery 切换() html 数据

jquery - 单击按钮元素内的复选框 - Firefox 问题

c# - 可以通过覆盖 DbContext.OnConfiguring 来配置提供者

javascript - 同一个js函数可以同时进行AJAX调用吗?

javascript - Ajax表单提交两次

javascript - 如何在我的输入字段中只允许数字、一个逗号和逗号后的两位数字?

javascript - 无法返回动态创建的复选框的值

asp.net - 将ASP.NET Identity升级到2.0后出现异常