0

现在解决了

嗨,我正在通过 Ajax 调用 ac# web 方法。

我想在 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
                });

而网络方法是

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


    }

谢谢

4

1 回答 1

0

回答

啊解决了!!!

我现在在 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

于 2014-05-01T13:11:19.543 回答