简体   繁体   中英

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

I am developing a quite large application. But I am facing problem in Ajax. For that I tried to make a new and short program to invoke Ajax for test purpose. But I am stuck in that. Here is the Test.aspx code

<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

$(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

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

When I put some value in TextBox. It alerts Yes!. But does not invoke [WebMethod]. Anyone know the problem.

Make your [WebMethod] static as

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

change ajax data value to data: "{'str':'" + text + "'}"

UPDATE Try This Same Code 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);
        }
    });
   });
});

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

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

This is working

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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