2

If someone could point out what's wrong here I would certainly appreciate it. I can set a breakpoint in the webmethod and it gets hit, but it always errors out.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAX_Test._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>AJAX Test</title>
        <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
    </head>
    <body>
        <form id="form1" runat="server">
            <button id="theButton">Click Here</button>
        </form>
        <script type="text/javascript">
            $(function () {
                $("#theButton").on("click", function () {
                    $.ajax({
                        type: "POST",
                        url: "AjaxWebService.asmx/HelloWorld",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: "{}",
                        success: AjaxSucceeded,
                        error: AjaxFailed
                    });
                });
            });

            function AjaxSucceeded(data, status) {
                alert("success");
            }

            function AjaxFailed(jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            }
        </script>
    </body>
</html>

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace AJAX_Test
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class AjaxWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}
1
  • I altered the code, and now the third arg throws an internal server error. I see there's an answer below involving [ScriptService] and I'll take a look at that. Commented Nov 30, 2011 at 18:37

2 Answers 2

4

Here is your problem:

Add this attribute to your service:

[ScriptService]

and add this to your method:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

When you connect to a service from javascript it must have those attributes.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding the [ScriptService] fixed it without the [ScriptMethod... any idea why?
In any case, thanks much. I certainly wasn't finding the missing piece.
Per MSDN - "The ScriptMethodAttribute attribute is optional. (However, methods that can be called from client script must have the System.Web.Services.WebMethodAttribute attribute applied.). If a method is not marked with ScriptMethodAttribute, the method will be called by using the HTTP POST command and the response will be serialized as JSON. You cannot override this setting from script." It looks like it does what you want it to by default. Learn somthing everyday, I always have used the attribute, guess its optional.
0

You're telling jquery to expect JSON data as a response from the ajax server, but you're sending a bare string as a response. a JSON string would be "Hello World", and the server-side code should be:

return "\"Hello World\"";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.