开发者

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

开发者 https://www.devze.com 2023-04-04 10:18 出处:网络
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 [Scrip

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;
        }
    }
}

as开发者_如何学Gopx 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


just add Attribute Class [ScriptService]

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]


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


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!


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 .


Just adding this in case someone else made the same stupid mistake I did.

Solution: Right click on the .asmx file in Visual Studio's Solution Explorer and select "View Markup". Verify that the value for the Class attribute is correct and not pointing to the wrong class. For some reason, the mismatch of the CodeBehind file and the Class name will throw this error instead of something more relevant.

Back story: Working on legacy code and wanted to use an .asmx web service to remain consistent with the rest of the code base. In VS2017, I couldn't find where to add that file so I just copied another .asmx file and renamed it. I should have known better. The issue is slightly masked because you are taken directly to the code behind file when you double click on the .asmx in the solution explorer, so it's easy to overlook that renaming the file does not update the Class attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号