如何使用Jquery从ASP.Net WebMethod获取数组到Ajax?

时间:2018-04-12 09:15:55

标签: jquery asp.net ajax

我从ajax调用web方法,我想返回一个数组。我试过以下代码:

$.ajax({
  type: "POST",
  contentType: "application/json;charset=utf-8",
  url: "adminCopyCrystalReport.aspx/getAllDirectoryNames",
  //data: JSON.stringify({
  //    //"dirLocation": $(this).children('.pathValue').text()
  //    "dirLocation": correctPath
  //}),
  dataType: "json",
  success: function (data) {
    alert('Success = ' + data.d);
  },
  error: function (result) {
    alert("Error");
  }
});
public static GetFolderDetails[] getAllDirectoryNames()
{
  string paths = "~CrystalReportFiles/";
  List<GetFolderDetails> details = new List<GetFolderDetails>();

  foreach (var dirr in new DirectoryInfo(System.Web.Hosting.HostingEnvironment.MapPath(@paths)).GetDirectories())
  {
    //dir.Add(dirr.Name);
  }

  GetFolderDetails fd = new GetFolderDetails();
  fd.fullFolder = "xxx";
  fd.folderName = "ccc";
  fd.urlPath = "eee";

  return details.ToArray();
}

它会引发500内部服务器错误。如果我返回一个字符串,我得到输出

2 个答案:

答案 0 :(得分:0)

尝试序列化数组(使用p)并使用Newtonsoft

Response.Write()

答案 1 :(得分:0)

也许你正在低于移民

Global.asax文件内的Application_Start方法评论中

//RouteConfig.RegisterRoutes(RouteTable.Routes);

确保EnablePageMethods

中的ScriptManager
<asp:ScriptManager runat="server" EnablePageMethods="true">