views:

346

answers:

1

I have a really simple AJAX method inside my Default.aspx.cs and it looks like this:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string GetDate()
    {
        return DateTime.Now.ToString();
    }
}

And the Default.aspx looks like this:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="http://192.168.1.2/tizma.com/js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">   
        $(document).ready(function() {
          // Add the page method call as an onclick handler for the div.
          $("#Result").click(function() {
            $.ajax({
              type: "POST",
              url: "Default.aspx/GetDate",
              data: "{}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: AjaxSucceeded,
              error: AjaxFailed
            });
          });
        });

        function AjaxSucceeded(result)
        {
            alert(result.d);
        }

        function AjaxFailed(result)
        {
            alert(result.status + " " + result.statusText);
        }
    </script>
</head>
<body>
    <div id="Result">Click me</div>
</body>
</html>

The problem is when I click the div the ajax error function is all that is ever called with a 200 status.

What am I doing wrong?

A: 

Not sure if it's causing the problem but you have a line which reads:

data: "{}",

it should read:

data: {},

or you can omit the line altogether as it's an optional parameter to the method call. You are currently setting it to a string value when it actually expects parameters for the webmethod which might cause problems.

Also, the lines reading:

contentType: "application/json; charset=utf-8",
dataType: "json",

seem unnecessary to me because for starters, it isn't obvious that your webmethod is actually returning json. I think it's just returning a string. Try removing those three lines alltogether and see if it doesn't just start working.

grenade
I tried taking those three lines out and although the success method fires the actual webmethod doesn't and the success function gets a result of the entire web page and the result.d is undefined.
Andy
OK, we're making progress. In that case, the simplest fix is going to be to create a new page. Something like Services.aspx. Make sure it does not use a master page and take out any html that is generated by Visual Studio the .aspx page should only have the first line (Page directive) then move your webmethod into that page, update the url in the ajax call and see how we get on.
grenade
I think we've been going down the wrong path. I've just read an article about the result.d. It appears this was a change in .NET3.5. My site was set to .NET2.0 changing it to 3.5 made it work instantly.My question now is what do I need to change to make this work under .NET2.0
Andy