2013-05-28 21 views
0

現在問題解決了看到如果一個AJAX調用的SoapException

嗨,我呼籲通過Ajax C#的Web方法後返回true或false。

我想在Ajax中處理返回的true和false值,但我似乎無法找到詢問我返回的數據的方式。

對不起,如果這是一個簡單的問題,我是相當新手。

我的代碼是

$.ajax({ 
        url: "Subscriptions.aspx/AddSub", 
        data: "{ 'strReportPath': '" + Path + 
          "' , strEmail: '" + $('#EmailAddress').val() + 
          "' , strDayofWeek: '" + daysSelected + 
          "' , strInterval: '" + intervalSelected + 
          "' , intTimeofDay: '" + timeofDay + 
          "' , strDayofMonth: '" + dayofMonth + 
          "'}", 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json; charset=utf-8", 
        success: function (data) { 

         if (data[0] == true) { 
          alert("Subscription added"); 
         } else { 
          alert("There has been a error"); 
         } 

         // Enable button again 
         $(".AddSub").removeAttr("disabled"); 
        }, 
        error: function (xhr, status, err) { 
         alert("Error adding subscription: " + err); 

         // Enable button again 
         $(".AddSub").removeAttr("disabled"); 
        }, 
        async: false 
       }); 

和Web方法是

 [WebMethod] 
    public static bool AddSub(string strReportPath, string strEmail, string strDayofWeek, string strInterval, int intTimeofDay, int strDayofMonth) 
    { 
     // Create webservice object 
     ReportService2005.ReportingService2005 rs = new ReportingService2005(); 
     rs.Credentials = System.Net.CredentialCache.DefaultCredentials; 

     try 
     { 

      // Make sure their is a semi colon at the end of the email 
      if (strEmail.EndsWith(";")) 
      { 
       // Do nothing 
      } 
      else 
      { 
       strEmail = strEmail + ";"; 
      } 
      string _reportName = strReportPath; 

      DateTime topDatetime = DateTime.Now; 

      ExtensionSettings extensionSettings = new ExtensionSettings(); 
      List<ParameterValue> extParameters = new List<ParameterValue>(); 
      List<ParameterValue> parameters = new List<ParameterValue>(); 
      string description = "Email: " + strEmail; 
      string eventType = "TimedSubscription"; 

      extensionSettings.Extension = "Report Server Email"; 



      // If report is monthly default its run time to 7am 
      if (strInterval == "Monthly") 
      { 
       intTimeofDay = 7; 
      } 

      string scheduleXml = "<ScheduleDefinition><StartDateTime>" + topDatetime.ToString("yyyy-MM-dd") + "-" + (intTimeofDay-1) +":00" + "</StartDateTime>"; 

      // Set up the timing of the report. 
      switch(strInterval) 
      { 
       case "Daily": 
         scheduleXml += "<WeeklyRecurrence>" + 
             "<WeeksInterval> 1 </WeeksInterval>" + 
             "<DaysOfWeek>" + "<Monday>true</Monday>" + 
                  "<Tuesday>true</Tuesday>" + 
                  "<Wednesday>true</Wednesday>" + 
                  "<Thursday>true</Thursday>" + 
                  "<Friday>true</Friday>" + "</DaysOfWeek>" + 
            "</WeeklyRecurrence>"; 
         break; 

       case "Weekly": 
         scheduleXml += "<WeeklyRecurrence>" + 
              "<WeeksInterval> 1 </WeeksInterval>" + 
              "<DaysOfWeek>" + strDayofWeek + "</DaysOfWeek>" + 
             "</WeeklyRecurrence>"; 
         break; 

       case "Monthly": 
         scheduleXml += "<MonthlyRecurrence>" + 
              "<Days>" + strDayofMonth + "</Days>" + 
              "<MonthsOfYear>" + 
               "<January>true</January>" + 
               "<February>true</February>" + 
               "<March>true</March>" + 
               "<April>true</April>" + 
               "<May>true</May>" + 
               "<June>true</June>" + 
               "<July>true</July>" + 
               "<August>true</August>" + 
               "<September>true</September>" + 
               "<October>true</October>" + 
               "<November>true</November>" + 
               "<December>true</December>" + 
              "</MonthsOfYear>" + 
             "</MonthlyRecurrence>"; 
         break; 
      } 

      scheduleXml += "</ScheduleDefinition>"; 

      extParameters.Add(new ParameterValue() { Name = "RenderFormat", Value = "EXCELOPENXML" }); 
      extParameters.Add(new ParameterValue() { Name = "TO", Value = strEmail }); 
      extParameters.Add(new ParameterValue() { Name = "IncludeReport", Value = "True" }); 
      extParameters.Add(new ParameterValue() { Name = "Subject", Value = "subject - " + " (" + strReportPath + ")" }); 

      extensionSettings.ParameterValues = extParameters.ToArray(); 

      //Create the subscription 
      rs.CreateSubscription(_reportName, extensionSettings, description, eventType, scheduleXml, parameters.ToArray()); 

      // Success 
      return true; 
     } 

     catch(SoapException e) 
     { 
      // Failure 
      return false; 
     } 


    } 

謝謝

+0

其實你不需要做變量返回的數據,僅返回「真」或「假」 – Bobby

+0

請創建一個問題的答案,並從問題中移除。其他人更容易告訴問題已經解決。 – demongolem

+0

感謝您的評論。我剛剛做到了。鮑比 – Bobby

回答

0

ANSWER

啊解決它!

我現在在Web方法

//Create the subscription 
      rs.CreateSubscription(_reportName, extensionSettings, description, eventType, scheduleXml, parameters.ToArray()); 

      string bob = "true"; 
      // Success 
      return bob; 
     } 

     catch(SoapException e) 
     { 
      string bob = "false"; 
      // Failure 
      return bob; 
     } 

然後在AJAX使用的名稱其次.D後綴作爲一個字符串變量返回的數據。

     success: function (bob) { 

         if (bob.d == "true") { 
          alert("Subscription added"); 
         } else { 
          alert("There has been a error"); 
         } 

感謝StackOverflow的