在webservice中使用Jquery AJAX调用方法时,尝试使用实体数据模型生成的webservice return 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>
有什么方法可以解决这个问题吗?请帮帮我。谢谢
发布于 2012-01-16 07:33:44
老问题。我也遇到了同样的问题,只是从ajax调用中删除了contenttype,它就可以工作了。
发布于 2012-08-17 20:51:20
我刚刚收到了完全相同的错误消息:“只有在类定义上具有ScriptService属性的Web services才能从脚本中调用。”,但是它有完全不同的原因和解决方案。
它在我的开发机器上工作,但不是在生产环境中。
在我的web.config中,我有:
<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>
将Add标记替换为较新的程序集版本:
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
它成功了!显然,较旧的程序集(1.0.61025.0)没有重新识别针对较新的(3.5.0.0)程序集编译的属性。
希望我能为某人节省我需要的时间来弄清楚这件事!
发布于 2020-03-15 15:17:32
我也面临着同样的问题。
我刚刚对下面的代码进行了注释,它开始工作了:
[System.Web.Script.Services.ScriptService]
在.asmx文件中可用。
https://stackoverflow.com/questions/7406485
复制相似问题