Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I attempt to use webservice return POCO class generated from entity data model as JSON when using Jquery AJAX call method in webservice. but I have problem with error "Only Web services with a [ScriptService] attribute on the class definition can be called from script", and getting stuck in it,

Here is my code :

namespace CarCareCenter.Web.Admin.Services
{
    /// <summary>
    /// Summary description for About
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class About : System.Web.Services.WebService
    {
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public static Entities.Category getAbout()
        {
            Entities.Category about = new Entities.Category();
            using (var context = new CarCareCenterDataEntities())
            {
                about = (from c in context.Categories where c.Type == "About" select c).SingleOrDefault();
            }

            return about;
        }
    }
}

aspx page :

<script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                url: '/Services/About.asmx/getAbout',
                data: '{}',
                success: function (response) {
                    var aboutContent = response.d;
                    alert(aboutContent);
                    $('#title-en').val(aboutContent.Name);
                    $('#title-vn').val(aboutContent.NameVn);
                    $('#content-en').val(aboutContent.Description);
                    $('#content-vn').val(aboutContent.DescriptionVn);
                    $('#id').val(aboutContent.CategoryId);
                },
                failure: function (message) {
                    alert(message);
                },
                error: function (result) {
                    alert(result);
                }
            });



            $('#SaveChange').bind('click', function () { updateAbout(); return false; });
            $('#Reset').bind('click', function () { getAbout(); return false; })
        });

        function updateAbout() {
            var abt = {
                "CategoryId": $('#id').val(),
                "Name": $('#title-en').val(),
                "NameVn": $('#title-vn').val(),
                "Description": $('#content-en').val(),
                "DescriptionVn": $('#content-vn').val()
            };
            $.ajax({
                type: "POST",
                url: "AboutManagement.aspx/updateAbout",
                data: JSON.stringify(abt),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var aboutContent = response.d;
                    $('#title-en').val(aboutContent.Name);
                    $('#title-vn').val(aboutContent.NameVn);
                    $('#content-en').val(aboutContent.Description);
                    $('#content-vn').val(aboutContent.DescriptionVn);
                },
                failure: function (message) {
                    alert(message);
                },
                error: function (result) {
                    alert(result);
                }
            });
        }
    </script>

Do any approaches to solve it ? Please help me . Thanks

like image 709
NevenHuynh Avatar asked Sep 13 '11 18:09

NevenHuynh


4 Answers

just add Attribute Class [ScriptService]

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
like image 156
Mahmoude Elghandour Avatar answered Nov 17 '22 01:11

Mahmoude Elghandour


old question. i had same problem, just removed contenttype from ajax call and it worked.

like image 37
MLS Avatar answered Nov 17 '22 00:11

MLS


I just had exactly this same error message: "Only Web services with a [ScriptService] attribute on the class definition can be called from script.", but it had a totally different cause and solution.

It was working on my development machine, but not in production.

In my web.config I had:

<system.web>
  <httpHandlers>
    <remove verb="*" path="*.asmx"></remove>
    <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35"></add>
  </httpHandlers>
</system.web>

Replaced the Add tag with a newer assembly version:

<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

And it worked! Aparently the older assembly (1.0.61025.0) did not reccognise the attribute that was compiled against the newer (3.5.0.0) one.

Hope I can save someone the hours I needed to get to the bottom of this one!

like image 25
Louis Somers Avatar answered Nov 17 '22 01:11

Louis Somers


I was also facing the same issue.
I just commented below code and it started working:

[System.Web.Script.Services.ScriptService]

available in .asmx file .

like image 45
Sarad Vishwakama Avatar answered Nov 17 '22 01:11

Sarad Vishwakama