如何将一个对象数组传递给jQuery Ajax理解的格式?

我有一个具有以下function的asmx Web服务:

[WebMethod] public List GetTags() { List TagList = new List(); DataTable dt = Helpers.Tags.GetTags(); foreach (DataRow dr in dt.Rows) { Tag t = new Tag(); t.TagName = dr["Tag"].ToString(); t.TagDescription = dr["Description"].ToString(); TagList.Add(t); } return TagList; } 

这是标签类:

  public class Tag { public string TagName { get; set; } public string TagDescription { get; set; } } 

GetTags()方法返回一个对象List标签对象。

我在jQuery中有一个AutoComplete函数,它接受以下格式的数组:

  var availableTagsCustom = [ { tagName: 'Ruby', tagDescription: 'Ruby is an open-source dynamic...' }, { tagName: 'Scala', tagDescription: 'Scala is a general purpose programming language...' }, { tagName: 'Scheme', tagDescription: 'Scheme is a functional programming language....' } ]; 

如何将我收到的“标记对象列表对象”转换或转换为此格式?

谢谢。

首先,您需要在服务类级别设置[System.Web.Script.Services.ScriptService]属性才能获得Json结果。

其次,您需要在方法上应用[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

 [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public List GetTags() { List TagList = new List(); DataTable dt = Helpers.Tags.GetTags(); foreach (DataRow dr in dt.Rows) { Tag t = new Tag(); t.TagName = dr["Tag"].ToString(); t.TagDescription = dr["Description"].ToString(); TagList.Add(t); } return TagList; } 

第三集Content-type: application/json in request。

本文也可能有用“使用jquery来使用aspnet json web服务