如何解决只能从脚本中调用类定义中具有[ScriptService]属性的Web服务

Nev*_*ynh 20 error-handling web-services

当在webservice中使用Jquery AJAX调用方法时,我尝试使用从实体数据模型生成的webservice返回POCO类作为JSON.但我有错误的问题"只能从脚本中调用类定义上有[ScriptService]属性的Web服务",并且卡在其中,

这是我的代码:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

aspx页面:

<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>
Run Code Online (Sandbox Code Playgroud)

有办法解决吗?请帮我 .谢谢

Mah*_*our 53

只需添加属性类[ScriptService]

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
Run Code Online (Sandbox Code Playgroud)

  • 它应该是`[System.Web.Script.Services.ScriptService]`,没有星号.另请注意,WebMethods被声明为`public`并且是实例方法,而不是声明为`public static`的PageMethods. (5认同)

MLS*_*MLS 5

老问题.我有同样的问题,只是从ajax调用中删除了contenttype并且它有效.