【问题标题】:Ajax Call in Asp.net ([WeBMethod] Function not calling)Asp.net 中的 Ajax 调用([WebMethod] 函数未调用)
【发布时间】:2015-11-26 13:20:37
【问题描述】:

我正在开发一个相当大的应用程序。但是我在 Ajax 中遇到了问题。为此,我尝试制作一个新的简短程序来调用 Ajax 以进行测试。但我陷入了困境。 这是Test.aspx代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Testing</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="test.js"></script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
    </form>
</body>
</html>

Ajax 函数是

$(document).ready(function () {
$("#Button1").click(function () {
    var text = $("#TextBox1").val();
    text = "this is test";
    debugger
    $.ajax({
        type: "POST",
        contentype: "application/json; charset=utf-8",
        url: "Test.aspx/Test",
        data: { str: text},
        //dataType:"json",
        success: function (data) {
            alert("yes");
        },
        error: function (response) {
            debugger
            alert(response);
        }
    });
    return false;
});
});

下面是Test.aspx.cs代码

[WebMethod]
    public void Test(string str)
    {
        Console.WriteLine(str);
    }

当我在 TextBox 中添加一些值时。它会提醒是的!但不调用 [WebMethod]。 任何人都知道这个问题。

【问题讨论】:

  • 我改成静态的了。但问题依然存在。
  • 在成功回调中使用Console.WriteLine(str); 而不是return str; 然后alert(data)

标签: javascript c# jquery asp.net ajax


【解决方案1】:

这是有效的

C#

[WebMethod]
public static string Test(string str)
{
      return str;
}

JS

const text = "this is test";   
$.ajax({      
      url: "Test.aspx/Test?str="+text,
      contentType: "application/json; charset=utf-8",
      method: 'post',
      data: "{'str':'"+text+"'}",
      success: function (data) {
               console.log(data);},
      error: function (response) {
               debugger;
               console.log(response);  }
});

【讨论】:

    【解决方案2】:

    将您的[WebMethod] 设为静态

    [WebMethod]
        public static void Test(string str)
        {
            //Console.WriteLine(str);
            string retstr=str;
        }
    

    将 ajax 数据值更改为 data: "{'str':'" + text + "'}"

    更新 试试这个相同的代码 aspx:

    <asp:Button ID="Button1" ClientIDMode="Static" runat="server" Text="Button" />
    

    aspx.cs

        [WebMethod]
        public static void Test(string str)
        {
            string abc=str;//Use this wherever you want to, check its value by debugging
        }
    

    test.js

    $(document).ready(function () {
    $("#Button1").click(function () {
        var text = "this is test";
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Test.aspx/Test",
            data: "{'str':'" + text + "'}",
            dataType:"json",
            success: function (data) {
                alert("yes");
            },
            error: function (response) {
                alert(response);
            }
        });
       });
    });
    

    【讨论】:

    • 数据类型应该是 dataType: "json"
    • 将 Console.Writeline 更改为某个变量或返回类型,检查我的编辑 ^
    • 当我将 dataType 更改为 json 时。然后是ajax调用的错误函数。
    • 添加 ClientIDMode="Static" 到 Button 如果您使用 .net 4.0 或在 jquery 或 javascript 中调用时使用 。
    • @FawadWarraich 如果有帮助,您可以将其标记为答案
    【解决方案3】:

    您是否尝试过为您的方法设置 ScriptMethod 属性,如下所示:

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    

    【讨论】:

    • 是的,我试过了。我在这段代码中被困了三天。
    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多