gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:01:41 25 4
gpt4 key购买 nike

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

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>

有什么办法可以解决吗?请帮我 。谢谢

最佳答案

只需添加属性类 [脚本服务]

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

关于web-services - 如何解决只有在类定义上具有[ScriptService]属性的Web服务才能从脚本中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7406485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com