简体   繁体   English

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

[英]Error when I am calling webservice with json parameter

I have a web service that save ssrs report. 我有一个保存ssrs报告的Web服务。 In the parameters of the web service I got json. 在Web服务的参数中,我得到了json。 The web service code: 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; }
    }
}

The jquery code that call the call the web service is: 调用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}")
          }
      })
  })

The web service installed on the IIS. 在IIS上安装的Web服务。 when I am trying to run the jquery I'm getting this error: 当我尝试运行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)

Does any one can help me with that please?? 请问有人可以帮助我吗?

Your problem is in jquery you are creating single elemen array not multiple element array and then you are accessing in c# code as multiple element array but the way you have did in jquery it can't go further more then 0 index in c#. 您的问题是在jquery中创建单个元素数组而不是多元素数组,然后在c#代码中将其作为多元素数组访问,但是在jquery中所做的操作不能再使0在c#中变索引。 Because at zero index you have fetched your whole element. 因为在零索引处,您已经获取了整个元素。 You need to do it like. 您需要这样做。

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                  }]
                  ]
                };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM