I am trying to do a very simple task. Not sure what I am doing wrong. I need to pass some values to code behind method, do some calculations there and then return the result. I started with a test method. There are many examples on web. But nothing working for me.
Please see my default page code below:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication7._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$(document).ready(function () {
var loc = window.location.href;
$("#btnClick").click(function (event) {
$.ajax({
type: 'POST',
url: loc + "/GetMessage",
data: "{}",
contentType: "application/json; charset=utf-8"
})
.success(function (response) {
alert(response.d);
})
.error(function (response) {
alert(response.d);
});
});
});
</script>
<input id="btnClick" type="button" value="button" />
</asp:Content>
The jquery has been referenced in master page:
<script src="Scripts/jquery-2.1.0.js"></script>
<script src="Scripts/jquery-2.1.0.min.js"></script>
codebehind:
[WebMethod]
public static string GetMessage()
{
return "test";
}
I do get "Undefined" error. When I change the .success and .error function like below
.success(function () {
alert("S");
})
.error(function () {
alert("E");
});
It shows "S". My understanding is that it finds the code behind method. But the 'response' is undefined. Does it mean the code behind method is not returning the data in json format?

dataTypeheader for your return type.jquery-2.1.0.jsorjquery-2.1.0.min.js. I would recommend.minversionreturn "test"toreturn "{\"test\":\"test\"}";