接受List的ASP.NET Web方法失败,“Web服务方法名称无效”.
我想创建一个接受自定义对象列表的Web方法(通过jQuery /
JSON传入).
当我在本地运行网站时,一切似乎都有效. jQuery和ASP.NET,每个人都很高兴.但当我把它放在我们的一台服务器上时,它会爆炸.在ajax请求之后,jQuery获得500错误,响应为:
这是Web服务方法: [WebMethod] public void EditCustomObjects(int ID,List<CustomObject> CustomObjectList) { // Code here } 我的jQuery代码(我觉得不重要,因为错误似乎发生在Web服务级别): var data = JSON.stringify({ ID: id,CustomObjectList: customObjectList }); $.ajax({ type: "POST",url: "/manageobjects.asmx/EditCustomObjects",data: data,contentType: "application/json; charset=utf-8",async: false,dataType: "json",success: function(xml,ajaxStatus) { // stuff here } }); customObjectList初始化如下: var customObjectList = []; 我像这样添加项目(通过循环): var itemObject = { ObjectTitle = objectTitle,ObjectDescription = objectDescription,ObjectValue = objectValue } customObjectList.push(itemObject); 那么,我在这里做错了吗?有没有更好的方法将数据数组从jQuery传递到ASP.NET Web服务方法?有没有办法解决“Web服务方法名称无效”.错误? 仅供参考,我在Windows Server 2003机器上运行.NET 2.0,并从此站点获取上述代码:http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/ 编辑:有人要求更多关于网络服务的信息,我宁愿不提供整个班级,但这里有更多可能会有所帮助: [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class ManageObjects : Custom.Web.UI.Services.Service { } 巴拉 解决方法
我根据您可以直接在浏览器中访问Web服务的注释进行分组.
只是为了将自定义对象与配置隔离开来,您可以放置??另一个服务,例如: [WebMethod] public static string GetServerTimeString() { return "Current Server Time: " + DateTime.Now.ToString(); } 从客户端调用jQuery ajax调用.如果这样可行,那么它可能与您的对象有关,而不是在服务器端配置.否则,继续查看服务器端配置跟踪. 编辑:一些示例代码: [WebMethod(EnableSession = true)] public Category[] GetCategoryList() { return GetCategories(); } private Category[] GetCategories() { List<Category> category = new List<Category>(); CategoryCollection matchingCategories = CategoryList.GetCategoryList(); foreach (Category CategoryRow in matchingCategories) { category.Add(new Category(CategoryRow.CategoryId,CategoryRow.CategoryName)); } return category.ToArray(); } 以下是我发布复杂数据类型JSON值的示例 [WebMethod] public static string SaveProcedureList(NewProcedureData procedureSaveData) { ...do stuff here with my object } 这实际上包含了两个对象数组……我的NewProcedureData类型是在一个类中定义的. EDIT2: 以下是我在一个实例中处理复杂对象的方法: function cptRow(cptCode,cptCodeText,rowIndex) { this.cptCode = cptCode; this.cptCodeText = cptCodeText; this.modifierList = new Array(); //...more stuff here you get the idea } /* set up the save object */ function procedureSet() { this.provider = $('select#providerSelect option:selected').val(); // currentPageDoctor; this.patientIdtdb = currentPatientIdtdb;// a javascript object (string) //...more object build stuff. this.cptRows = Array(); for (i = 0; i < currentRowCount; i++) { if ($('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val() != watermarkText) { this.cptRows[i] = new cptRow($('.cptIcdLinkRow').eq(i).find('.cptCode').val(),$('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val(),i);//this is a javscript function that handles the array object }; }; }; //here is and example where I wrap up the object function SaveCurrentProcedures() { var currentSet = new procedureSet(); var procedureData = ""; var testData = { procedureSaveData: currentSet }; procedureData = JSON.stringify(testData); SaveProceduresData(procedureData); }; function SaveProceduresData(procedureSaveData) { $.ajax({ type: "POST",data: procedureSaveData,the rest of the ajax call... }); }; 注意!重要信息procedureSaveData名称必须与客户端和服务器端完全匹配才能使其正常工作. using System; using System.Collections.Generic; using System.Web; namespace MyNamespace.NewProcedure.BL { /// <summary> /// lists of objects,names must match the JavaScript names /// </summary> public class NewProcedureData { private string _patientId = ""; private string _patientIdTdb = ""; private List<CptRows> _cptRows = new List<CptRows>(); public NewProcedureData() { } public string PatientIdTdb { get { return _patientIdTdb; } set { _patientIdTdb = value; } } public string PatientId { get { return _patientId; } set { _patientId = value; } } public List<CptRows> CptRows = new List<CptRows>(); } -------- using System; using System.Collections.Generic; using System.Web; namespace MyNamespace.NewProcedure.BL { /// <summary> /// lists of objects,names must match the JavaScript names /// </summary> public class CptRows { private string _cptCode = ""; private string _cptCodeText = ""; public CptRows() { } public string CptCode { get { return _cptCode; } set { _cptCode = value; } } public string CptCodeText { get { return _cptCodeText; } set { _cptCodeText = value; } } } } 希望这可以帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-core – ASP.NET 5和MVC6中的project.json
- asp.net-mvc-3 – 重定向与RedirectToRouteResult不工作从其
- 在Asp.net中加载平衡,在开发时我应该考虑什么?
- asp.net – 空白WebResource.axd
- asp.net-core – 在发布ASP.NET Core应用程序时自动执行迁移
- asp.net – 访问母版页中的祖父母内容占位符
- asp.net-mvc – Chrome丢失了cookie
- asp.net-membership – 在另一个站点中实现Umbraco成员资格
- asp.net – 在Global.asax中使用Trace
- asp.net-mvc – 最佳实践:在web api控制器中处理错误和异常
- asp.net – 403从WindowsAzure.Storage生产错误
- vbscript – 尝试在.VBS文件中“包含”.ASP文件
- asp.net-membership – 泛型和ASP.Net成员资格提
- asp.net-mvc-3 – 增加ASP.NET MVC 3中的超时值
- 勾选复选框时,禁用一些ASP.Net验证控件
- asp-classic – 如何清除所有cookie?
- .NET Core技术研究系列-索引篇 .NET
- asp.net-mvc-4 – 从beta版升级后的Autofac / MV
- asp.net-core – 如何根据ASP.NET Core中的解决方
- asp.net – 将int数组发布到MVC控制器 – 什么是