当我使用json参数调用网络服务时出错

时间:2018-12-24 06:38:14

标签: jquery json web-services

我有一个保存ssrs报告的Web服务。在Web服务的参数中,我得到了json。 Web服务代码:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService]

    public class Save_Report : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld(NameValue[] Param)
        {

            CreateEventLog();

            EventLog.WriteEntry("UWQ_ReportService", Param.ToString());

            ReportingService2010 service = new ReportingService2010();

            ReportExecutionService rs = new ReportExecutionService();


            rs.Url = "http://rshrept16/reportserver/reportexecution2005.asmx?wsdl";
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;


            string Report_Path = "/Tutorial/{0}";
            string Destination_Path = "//rshfsc3/users3/NonMekorot/o-tsoudry/Tehila/{0}.{1}";

            string New_File_Name = "myReport1";
            string File_Name = "Sales Orders";
            string Suffix_File = "PDF";
            string historyID = null;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            reportexecution2005.Warning[] warnings;
            ExecutionHeader execHeader = new ExecutionHeader();
            ExecutionInfo execInfo = new ExecutionInfo();

            reportexecution2005.ParameterValue[] parameters = new reportexecution2005.ParameterValue[7];

            //EventLog.WriteEntry("UWQ_ReportService", string.Format("The Name parameter = {0}, the ID_l_Zones parameter = {1}", Name, ID_l_Zones));

            parameters[0] = new reportexecution2005.ParameterValue();
            parameters[0].Name = "ID_l_Date_From";
            parameters[0].Value = "01/01/2016 00:00:00";

            parameters[1] = new reportexecution2005.ParameterValue();
            parameters[1].Name = "ID_l_Date_To";
            parameters[1].Value = "01/03/2016 00:00:00";

            parameters[2] = new reportexecution2005.ParameterValue();
            parameters[2].Name = "ID_l_Zones";
            parameters[2].Value =  "82,83,84,85,86,87,88";

            parameters[3] = new reportexecution2005.ParameterValue();
            parameters[3].Name = "ID_l_Water_Source_Types";
            parameters[3].Value = null;

            parameters[4] = new reportexecution2005.ParameterValue();
            parameters[4].Name = "ID_l_Water_Types";
            parameters[4].Value = null;

            parameters[5] = new reportexecution2005.ParameterValue();
            parameters[5].Name = "ID_l_Water_Sources_Group_ID";
            parameters[5].Value = null;

            parameters[6] = new reportexecution2005.ParameterValue();
            parameters[6].Name = "ID_l_Parameters_Group_ID";
            parameters[6].Value = null;

            rs.ExecutionHeaderValue = execHeader;

            execInfo = rs.LoadReport(string.Format(Report_Path, File_Name), historyID);

            rs.SetExecutionParameters(parameters, "en-us");

            string SessionId = rs.ExecutionHeaderValue.ExecutionID;
           // Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID)

            byte[] bytes = rs.Render("PDF", null, out mimeType, out encoding, out extension, out warnings, out streamIds);

            try
            {
                System.IO.File.WriteAllBytes(string.Format(Destination_Path, New_File_Name, Suffix_File), bytes);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("UWQ_ReportService", "Error occur in save report" + ex.Message);
            }

            return string.Format(Report_Path, File_Name);
        }

        void CreateEventLog()
        {
            try
            {
                if (!EventLog.SourceExists("UWQ_ReportService"))
                {
                    EventLog.CreateEventSource("UWQ_ReportService", "UWQ_ReportService");
                }
            }
            catch (Exception exc)
            {
                using (EventLog eventLog = new EventLog("Application"))
                {
                    eventLog.Source = "Application";
                    eventLog.WriteEntry("Can't Create source log massage:" + exc.Message, EventLogEntryType.Information, 101, 1);
                }
            }

        }
    }

    public class NameValue
    {
        public string name { get; set; }
        public string value { get; set; }
    }
}

调用该Web服务的jquery代码为:

$('button').click(function(){
      var data = {"Param":
                  [
                    {"name": "ID_l_Date_From"               ,"value" : "01/01/2016 00:00:00" },
                    {"name": "ID_l_Date_To"                 ,"value" : "01/03/2016 00:00:00" },
                    {"name": "ID_l_Zones"                   ,"value" : "82,83,84,85,86,87,88"},
                    {"name": "ID_l_Water_Source_Types"      ,"value" : null                  },
                    {"name": "ID_l_Water_Types"             ,"value" : null                  },
                    {"name": "ID_l_Water_Sources_Group_ID"  ,"value" : null                  },
                    {"name": "ID_l_Parameters_Group_ID"     ,"value" : null                  }
                  ]
                };

      $.ajax({
          url: "http://mekuwqtest01/Report_Service/Save_Report.asmx/HelloWorld",
          type:"POST",
          data:JSON.stringify(data),
          dataType: "json",
          ContentType  : "application/json; charset=utf-8",
          ProcessData : true,
          success: function(result){
              console.log(result)
          },
          error: function(error){
              console.log("Error ${error}")
          }
      })
  })

IIS上安装的Web服务。 当我尝试运行jQuery时,出现此错误:

 System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
   at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

请问有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您的问题是在jquery中创建单个元素数组而不是多元素数组,然后以c#代码作为多元素数组进行访问,但是在jquery中所做的方式不能再多于c#中的0索引。因为在零索引处,您已经获取了整个元素。您需要这样做。

var data = {"Param":
                  [
                    [{"name": "ID_l_Date_From"               ,"value" : "01/01/2016 00:00:00" }],
                    [{"name": "ID_l_Date_To"                 ,"value" : "01/03/2016 00:00:00" }],
                    [{"name": "ID_l_Zones"                   ,"value" : "82,83,84,85,86,87,88"}],
                    [{"name": "ID_l_Water_Source_Types"      ,"value" : null                  }],
                    [{"name": "ID_l_Water_Types"             ,"value" : null                  }],
                    [{"name": "ID_l_Water_Sources_Group_ID"  ,"value" : null                  }],
                    [{"name": "ID_l_Parameters_Group_ID"     ,"value" : null                  }]
                  ]
                };