当我在本地运行网站时,一切似乎都有效. jQuery和ASP.NET,每个人都很高兴.但当我把它放在我们的一台服务器上时,它会爆炸.在ajax请求之后,jQuery获得500错误,响应为:
System.InvalidOperationException: EditCustomObjects Web Service method name is not valid.
这是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 { }
巴拉
解决方法
只是为了将自定义对象与配置隔离开来,您可以放置另一个服务,例如:
[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名称必须与客户端和服务器端完全匹配才能使其正常工作.
EDIT3:更多代码示例:
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; } } } }
希望这可以帮助.