1

I'm facing the following issue: Whenever I click on the button, the alert shows undefined.

Web method:

    [WebMethod]
    public static string getTest()
    {
        return "testing success";
    }

Ajax script

    <script type="text/javascript">
        function getTest() {
            $.ajax({
                type: "POST",
                url: "main.aspx/getTest",
                data: "{}",
                datatype: "json",
                contenttype: "/application/json; charset=utf-8",
                success: function (msg) {

                    alert(msg.d);

                },
                error: function (data) {
                }
            });
        }
    </script>
wpp
  • 7,093
  • 4
  • 33
  • 65
Convict Moody
  • 781
  • 1
  • 9
  • 28

2 Answers2

1

datatype should be dataType and contenttype should be contentType. Also remove / from start of "/application/json; charset=utf-8"

$.ajax({
            type: "POST",
            url: "main.aspx/getTest",
            data: "{}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (msg) {

                alert(msg.d);

            },
            error: function (data) {
            }
        });
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

Remove these from your ajax call

datatype: "json",
contenttype: "/application/json; charset=utf-8",

More about these

Differences between contentType and dataType in jQuery ajax function

What is content-type and datatype in an AJAX request?

You can find more details in the jQuery Ajax documentation

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117