2

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.

2
  • I changed to static. But the problem still exist. Commented Sep 1, 2015 at 10:43
  • 1
    instead of Console.WriteLine(str); use return str; and then alert(data) in success callback Commented Sep 1, 2015 at 10:44

3 Answers 3

2

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);
        }
    });
   });
});
Sign up to request clarification or add additional context in comments.

5 Comments

datatype should be dataType: "json"
Change Console.Writeline to some variable or return type, Check My Edit ^
When i change the dataType to json. Then error function of ajax call.
add ClientIDMode="Static" to Button if you are usin .net 4.0 or use <%=Button.ClientID%> when you call in jquery or javascript.
@FawadWarraich you can mark it as answer if it helped
2

This is working

C#

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

JS

const text = "this is test";   
$.ajax({      
      url: "Test.aspx/Test",
      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 Comments

I can confirm this is working in asp.net 4.7.1 with WebForms and translated the server side code to VB.NET; If you attach to your .NET process in Visual Studio, you can also put some breakpoint and you will be able to observe how VS stops at the function where you set the breakpoint; If you don't want to stop the execution using breakpoints, you can also use System.Diagnostics.Debug.WriteLine("...") to printout messages to the Output console from Visual Studio, selecting showing output from Debug.
In fact, the parameter str in the url is NOT NEEDED, because you are already setting it in the data: part.
1

Have you tried setting ScriptMethod attribute for your method like so:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

1 Comment

Yeah, I have tried this. I am stuck in this code for three days.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.