【问题标题】:Call ASP.NET WebMethod using AJAX JSON使用 AJAX JSON 调用 ASP.NET WebMethod
【发布时间】:2014-06-06 23:08:15
【问题描述】:

我正在使用 VB.NET 代码隐藏在 ASP.NET 中对一个新的 Web 应用程序进行原型设计,该应用程序使用 JQuery 的 AJAX 调用 WebMethod,但我不断收到解析器错误。这是我的标记:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="testpage.aspx.vb" Inherits="WorkOrder_testpage" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="../jquery-ui-1.9.2.custom.min.css" rel="stylesheet" />
    <script src="../jquery-1.8.3.js"></script>
    <script src="../jquery-ui-1.9.2.custom.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnTest').click(function () {
                $.ajax({
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    url: 'testpage.aspx/testFunction',
                    //data: {a:'hello'},
                    dataType: 'json',
                    success: function (data) {
                        $('#lblDebug').text(data);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(textStatus + ": " + errorThrown);
                    }
                });
            });
            $('#btnTest').focus();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
        <span id="lblDebug"></span><input type="button" id="btnTest" value="TEST" />
    </form>
</body>
</html>

还有我的代码隐藏:

Imports System.Web.Services
Imports System.Web.Script.Serialization
Partial Class WorkOrder_testpage
    Inherits System.Web.UI.Page
    <WebMethod()> _
    Public Shared Function testFunction() As String
        Dim jsonSer As New JavaScriptSerializer
        testFunction = jsonSer.Serialize("hello")
    End Function
End Class

我已经尝试过的事情:

  • 省略数据类型和/或内容类型。这会导致 HTML 在成功时被传回
  • 成功输出结果时使用“data.d”而不是“data”。仅在省略 dataType 和/或 contentType 时有效,不显示数据。
  • 将 httpModules 元素添加到 web.config。没有变化,但我可能还缺少更多内容。
  • 以几乎所有可以想象的方式格式化我的返回数据。总是以 parsererror 结尾。它总是说“SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data”

我认为我错了:

我认为要么我的 JSON 格式仍然不正确,在这种情况下请告诉我,或者有一些我不知道的配置阻止我的 JSON 通过。

我知道这与其他大约 80 个问题相似,但到目前为止我发现没有一个问题似乎有任何帮助。

我的帮助来源:
asp.net jquery ajax json: Simple example of exchanging data PageMethods, jQuery and JSON http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ http://www.aspdotnet-suresh.com/2013/12/jquery-ajax-json-example-in-aspnet.html
还有更多。

2014 年 6 月 10 日更新

我在下面尝试了 Mun 的答案,结果相似/相同。如果有人使用相同的结构完成此操作,我想知道您必须在 web.config 文件中添加或配置多少(如果有)。

【问题讨论】:

    标签: jquery asp.net ajax json vb.net


    【解决方案1】:

    通过 PageMethods 而不是 jQuery 调用您的网络方法可能更容易。

    代码隐藏(在 C# 中,但很容易转换为 VB.NET):

    [WebMethod]
    public static string SayHello(string name)
    {
        return "Hello " + name;
    }
    

    网络表单:

    $("#btnTest").click(function() {
        PageMethods.SayHello("Test", function(result) {
          alert(result);  // Should show "Hello Test"
        });
    });
    

    您的 SayHello 方法的 Javascript 代理将由脚本管理器自动创建,允许在客户端调用它。

    【讨论】:

    • 谢谢,但我仍然收到 HTML。我使用了相同的格式。我还有什么遗漏的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多