接受List <CustomObject>的ASP.NET Web方法失败,"Web服务方法名称无效".

Bar*_*ara 6 asp.net jquery web-services asmx invalidoperationexception

我想创建一个接受自定义对象列表的Web方法(通过jQuery/JSON传入).

当我在本地运行网站时,一切似乎都有效.jQuery和ASP.NET,每个人都很高兴.但当我把它放在我们的一台服务器上时,它会爆炸.在ajax请求之后,jQuery获得500错误,响应为:

System.InvalidOperationException:EditCustomObjects Web服务方法名称无效.

这是Web服务方法:

[WebMethod]
public void EditCustomObjects(int ID, List<CustomObject> CustomObjectList)
{
  // Code here
}
Run Code Online (Sandbox Code Playgroud)

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

customObjectList初始化如下:

var customObjectList = [];
Run Code Online (Sandbox Code Playgroud)

我像这样添加项目(通过循环):

var itemObject = { 
  ObjectTitle = objectTitle,
  ObjectDescription = objectDescription,
  ObjectValue = objectValue
}

customObjectList.push(itemObject);
Run Code Online (Sandbox Code Playgroud)

那么,我在这里做错了吗?有没有更好的方法将数据数组从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 
{
}
Run Code Online (Sandbox Code Playgroud)

巴拉

Mar*_*iss 4

我根据评论做出假设,您可以直接在浏览器中访问网络服务。

只是为了将自定义对象与配置隔离,您可以放置​​另一个服务,例如:

[WebMethod]
public static string GetServerTimeString()
{
    return "Current Server Time: " + DateTime.Now.ToString();
}
Run Code Online (Sandbox Code Playgroud)

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

这是我发布复杂数据类型 JSON 值的示例

[WebMethod]
 public static string SaveProcedureList(NewProcedureData procedureSaveData)
 {
          ...do stuff here with my object
 }
Run Code Online (Sandbox Code Playgroud)

这实际上包括其中的两个对象数组...我的 NewProcedureData 类型是在一个类中定义的,该类列出了这些对象。

编辑2:

以下是我在一个实例中处理复杂对象的方法:

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",
            contentType: "application/json; charset=utf-8",
            data: procedureSaveData,
the rest of the ajax call...
        });
    };
Run Code Online (Sandbox Code Playgroud)

注意!重要的是 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; }
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。