简体   繁体   English

Asp.net 中的 Ajax 调用([WebMethod] 函数未调用)

[英]Ajax Call in Asp.net ([WeBMethod] Function not calling)

I am developing a quite large application.我正在开发一个相当大的应用程序。 But I am facing problem in Ajax.但是我在 Ajax 中遇到了问题。 For that I tried to make a new and short program to invoke Ajax for test purpose.为此,我尝试制作一个新的简短程序来调用 Ajax 以进行测试。 But I am stuck in that.但我陷入了困境。 Here is the Test.aspx code这是 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 function is 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;
});
});

And Test.aspx.cs code is below Test.aspx.cs 代码如下

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

When I put some value in TextBox.当我在 TextBox 中放入一些值时。 It alerts Yes!.它提醒是!。 But does not invoke [WebMethod].但不调用[WebMethod]。 Anyone know the problem.任何人都知道问题所在。

Make your [WebMethod] static as使您的[WebMethod]静态为

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

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

UPDATE Try This Same Code aspx:更新试试这个相同的代码 aspx:

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

aspx.cs 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测试.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);
        }
    });
   });
});

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

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

This is working这是工作

C# C#

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

JS 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);  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM