user2116780
user2116780

Reputation:

500 error getting object from web service by AJAX in ASP.NET

I am having a problem making an AJAX request to a web service.

I am trying to connect to the web service and get back an object to fill an HTML form with its data for editing.

The AJAX code:

$.ajax({
    type: "GET",
    url: "WebService.asmx/UpdateNewGroup",
    data: "{ id : '7'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        var myData = JSON.parse(data.d); // data.d is a JSON formatted string, to turn it into a JSON object
        alert("s");
    }
});

And the web service:

[WebMethod]
public MeGroup UpdateNewGroup(String id)
{
    MeGroup group = new MeGroup();
    return group;
}

This is the error I get when I send a request:

500 (Internal Server Error) 

The path of the web service is right, and I enabled POST and GET requests in the service's web.config.

What could I be missing?

Edit:

Here is the error message I found based on J0e3gan's comment:

Only Web services with a [ScriptService] attribute on the class definition can be called from script.

Upvotes: 1

Views: 1445

Answers (1)

J0e3gan
J0e3gan

Reputation: 8938

Based on the error details you shared in reply to my comment, add the ScriptService attribute to your service class as follows:

[WebService(Namespace = "http://yournamespace/")]
[ScriptService]
public class Your service
{
    [WebMethod]
    public MeGroup UpdateNewGroup(String id)
    {
        MeGroup group = new MeGroup();
        return group;
    }
}

Upvotes: 4

Related Questions