Контроллер AngularJS вызывает контроллер MVC - это успешно, но выдает ошибку, которая содержит ноль - PullRequest
0 голосов
/ 19 сентября 2018

У меня есть приложение AngularJS / MVC, которое вызывает метод контроллера MVC.Метод успешно отправляет электронное письмо, но переходит в функцию error, и переменная error имеет значение null.

enter image description here

Почему происходит сбой?Я пытался узнать больше об ошибке, используя другой вызов, но предупреждение не появляется при его использовании.Я думаю, потому что возвращаемое значение равно нулю и не может быть разрешено.

Вот метод контроллера AngularJS.

    $scope.BookMySeat = function (UserName, SeatNo) {
        if (UserName) {
             // The UserName is not empty. Check to see of it exists.
             dataService.ValidateUsername(UserName).success(function (output) {
                  if (output == -1)
                {
                    // The user name does NOT exist. Do the insert to the table.

                    $scope.onclick = !$scope.onclick;

                    console.log(UserName + SeatNo);

                    dataService.InsertSeat(UserName, slotNum, SeatNo).success(function (output) {
                        $scope.bookedSeatList = output;

                        var imageCollection = new Array(totalseat);
                        $scope.images = imageCollection;
                        $scope.imageUrlNotBooked = "../Images/RED.jpg";

                        $scope.bookcollscope = $scope.bookedSeatList;
                        console.log($scope.bookcollscope);
                        var imageCollection1 = new Array($scope.bookedSeatList);
                        $scope.images1 = imageCollection1;
                        $scope.imageUrlBooked = "../Images/GREEN.jpg";

                        // Alert.
                        alert("Seat number " + SeatNo + " has been booked for : " + UserName);

                        //----------------------------------------------------------------------------------------------------------
                        // Send an email using a MVC controller method.
                        // - Using $http to call to a MVC controller method.
                        //----------------------------------------------------------------------------------------------------------

                        var encodedQueryString = encodeURIComponent('emailAddress') + '=' + encodeURIComponent('dc4444@hotmail.com') + '&' + encodeURIComponent('type') + '=' + encodeURIComponent('I') + '&' + encodeURIComponent('seatNbr') + '=' + encodeURIComponent(SeatNo) + '&' + encodeURIComponent('slotNbr') + '=' + encodeURIComponent(0);
 
                        $http.get("/Home/SendAlertEmail?" + encodedQueryString).success(function (data) {
                            // Alert.
                            alert("An insert email was sent.");
                        }).error(function (error) {
                            // Error alert.
                            alert("Something went wrong with the insert - SendEmail. " + error);
                            console.log(error);
                        });

                        //$http.get("/Home/SendAlertEmail?" + encodedQueryString).success(function (data) {
                        //    // Alert.
                        //    alert("An insert email was sent.");
                        //}).error(function (xhr) {
                        //    // Error alert.
                        //    alert("Something went wrong with the insert - SendEmail. Status: " + xhr.status + ". Response Text: " + xhr.responseText);
                        //    console.log(error);
                        //});
                    }).error(function (output) {
                        $scope.error = true;
                    });
                }
                else
                {
                    // The user name exists.
                     alert("That passenger name exists. Please enter a UNIQUE passenger name in order to book a seat.");
                }
                console.log(output);
            }).error(function (output) {
                $scope.error = true;
            });
        }
        else
        {
            alert("The passenger name is empty. Please enter a passenger name in order to book a seat.");
        } 
    }

Вот метод контроллера MVC.

       public void SendAlertEmail([FromUri] string emailAddress, [FromUri]string type, [FromUri]int seatNbr, [FromUri]int slotNbr)
    {
        string strHtmlMessageBody = "";

        NetworkCredential NetworkCred = new NetworkCredential();
        System.Net.Mail.SmtpClient smtp = new SmtpClient();

        try
        {
            MailMessage mm = new MailMessage();

            mm.From = new MailAddress("xxxxx@hotmail.com");
            mm.Subject = "From the BookSeat Portal. Passenger Seat Information.";

            if (type == "I")
            {
                strHtmlMessageBody = "<p>Your passenger seat: " + seatNbr + " has been created by the system.</p>";
            }
            else
            {
                strHtmlMessageBody = "<p>Your passenger seat number: " + seatNbr + ", slot number: " + slotNbr + " has been deleted by the system.</p>";
            }

            mm.Body = strHtmlMessageBody;
            mm.IsBodyHtml = true;
            mm.To.Add(new MailAddress(emailAddress));

            smtp.Host = "smtp.live.com";
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            smtp.Port = 587;
            NetworkCred.UserName = "xxxxx@hotmail.com";
            NetworkCred.Password = "yyyyyyy";

            smtp.Credentials = NetworkCred;
            smtp.Send(mm);
        }
        catch (SmtpException smtpEx)
        {
            throw new Exception("SMTP exception: " + smtpEx);
        }
        catch (Exception ex)
        {
            throw new Exception("Sending email exception: " + ex);
        }
    }

Вот отправленное письмо.

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...