1

I'm new coding so, here's my question, I'm getting data from my DB through c# and angularJs I can modify my data and I want to sending back after I'm gonna need to save it on DB but I'm going step by step, so here's my code, thanks a lot. It is in spanish I hope you don't mind.

var datosActuales = { 
    'id': $scope.datosActualizados.id,
    'remitente': $scope.datosActualizados.remitente,
    'proveedor': $scope.datosActualizados.proveedor,
    'destinatario': $scope.datosActualizados.correoDestinatario,
    'copia': $scope.datosActualizados.copia,
    'mensaje': $scope.datosActualizados.mensaje
};

$http.post('http://localhost:65110/api/CorreosProveedor/CorreoActual', datosActuales)
.then(function (response) {
    console.log(response);
}, function (response) {
    console.log("error..");
});
// POST api/<controller>
[HttpPost]
public void CorreoActual(int id, string remitente, string proveedor, string destinatario, string mensaje, string copia)
{
    try
    {
        //string separa_copias = copia.Split(new char[] { ',' });
        ModeloCorreo correo_recibido = new ModeloCorreo();

        correo_recibido.id = id;
        correo_recibido.remitente = remitente;
        correo_recibido.nombre_proveedor = proveedor;
        correo_recibido.destinatario = destinatario;
        correo_recibido.mensaje = mensaje;
        // correo_recibido.correos_copia = separa_copias;
    }
    catch (Exception ex)
    {

    }
}

the error in console is

405 Method not allowed

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • 1
    Welcome to Stack Overflow! so what exactly is your question? what did you try and what error did you get? – timur Jan 07 '20 at 23:17
  • The `.success` method has [been removed from the AngularJS framework](https://stackoverflow.com/questions/35329384/why-are-angularjs-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). The `dataType` property is ignored. The is no need to use `JSON.stringify` as that is done automatically. – georgeawg Jan 08 '20 at 00:08
  • Hello! I'm trying to send back a json to asp.net and after i need to save it in db, I already change my JS code, the error in console is 405 Method not allowed – Fernando Compean Jan 08 '20 at 17:24

2 Answers2

0

you can try the code below :

$http({
    method: "POST",
    url: 'http://localhost:65110/api/CorreosProveedor/CorreoActual' 

    }).then(function successCallback(response) {
            console.log(response);
    }, function errorCallback(response) {
    console.log("error..");
    });

hope this will help you please comment if you have any queries

0

This is my final code and it is working fine.

First I receive a response from my DB then I edit my responses and send it back at the end ASP.NET is in charge to see if the response needs to be updated, deleted or inserted.

Thanks a lot.

        [Route("api/configuracorreo")]
        public void Post([FromBody]ModeloEditarCorreo correo)
        {
            try
            {

                ModeloEditarCorreo correo_recibido = correo;

                correo_recibido.id = correo.id;
                correo_recibido.remitente = correo.remitente;
                correo_recibido.nombre_proveedor = correo.nombre_proveedor;
                correo_recibido.destinatario = correo.destinatario;
                correo_recibido.mensaje = correo.mensaje;
                correo_recibido.correos_copia = correo.correos_copia;


                using (Certificados_UVM_DEVEntities dba = new Certificados_UVM_DEVEntities())
                {

                    Models.Conf_Correo correoExistente = dba.Conf_Correo.FirstOrDefault();


                    if (correoExistente == null)
                    {
                        string insertCorreo = string.Format("INSERT INTO Conf_Correo (remitente, nombre_proveedor, destinatario, mensaje) VALUES ('{0}', '{1}', '{2}', '{3}')", correo.remitente, correo.nombre_proveedor, correo.destinatario, correo.mensaje);
                        dba.Database.ExecuteSqlCommand(insertCorreo);


                        foreach (string correoUnico in correo.correos_copia)
                        {
                            Models.Conf_Correo correoCreado = dba.Conf_Correo.FirstOrDefault();
                            string insertEmails = string.Format("Insert INTO Conf_Correo_Destinatarios (id_conf_correo, correo_copia) VALUES ('{0}', '{1}')", '1', correoCreado.id);
                            dba.Database.ExecuteSqlCommand(insertEmails);
                        }

                    } else
                    {

                        string queryStoreString = string.Format("UPDATE Conf_Correo SET nombre_proveedor = '{0}', destinatario = '{1}', mensaje = '{2}' WHERE id = '{3}'", correo.nombre_proveedor, correo.destinatario, correo.mensaje, correo.id);
                        dba.Database.ExecuteSqlCommand(queryStoreString);

                        string deleteEmails = string.Format("DELETE FROM Conf_Correo_Destinatarios WHERE id_conf_correo = '{0}'", correo.id);
                        dba.Database.ExecuteSqlCommand(deleteEmails);

                        foreach( string correoUnico in correo.correos_copia)
                        {
                            string insertEmails = string.Format("Insert INTO Conf_Correo_Destinatarios (id_conf_correo, correo_copia) VALUES ('{0}', '{1}')", correo.id, correoUnico);
                            dba.Database.ExecuteSqlCommand(insertEmails);
                        }

                    }




                }
            }


            catch (Exception ex)
            {

            }

        }
        
        
         public class ModeloEditarCorreo
    {
        public int id { get; set; }
        public string remitente { get; set; }

        public string nombre_proveedor { get; set; }

        public string destinatario { get; set; }

        public string mensaje { get; set; }
        public string[] correos_copia { get; set; }


    }

 $http.get(DIR.hostSelected + 'api/configuracioncorreo').
      then(function(response) {


       var correos = response.data.correos_copia.map(function(e){
        return e["correo_copia"];
       });
       var correos_copia = correos.join(", ");

       $scope.isRemitenteDisabled = response.data.remitente ? true : false;

       $scope.datosOriginales = {
        id: response.data.id,
        remitente: response.data.remitente,
        proveedor: response.data.nombre_proveedor,
        correoDestinatario: response.data.destinatario,
        copia: correos_copia,
        mensaje: response.data.mensaje 
       };
 
      });

      
      $scope.validarCorreo = function() {
       if ($scope.correoDestinatario != $scope.confirmaCorreo) {
       $scope.IsMatch=true;
       return false;
       }
       $scope.IsMatch=false;
      };

      $scope.cancelar = function() {
       $window.location.reload();
      };


      $scope.actualizaDatos = function(){ 

       var ccActual = [];
       ccActual = $scope.datosOriginales.copia.split(", ");

       $scope.editaDatos = {};
       $scope.editaDatos = angular.copy($scope.datosOriginales);
       $scope.datosActualizados = $scope.editaDatos;
       var datosActuales = {
        'id': $scope.datosActualizados.id,
        'remitente': $scope.datosActualizados.remitente,
        'nombre_proveedor': $scope.datosActualizados.proveedor,
        'destinatario': $scope.datosActualizados.correoDestinatario,
        'correos_copia': ccActual,
        'mensaje': $scope.datosActualizados.mensaje
       };
       $scope.isDisabled = true;

       console.log($scope.datosActualizados);
       console.log(ccActual)

       var config = { headers: {'Content-Type': 'application/json;charset=utf-8'}}

       $http.post(DIR.hostSelected + 'api/configuracorreo', datosActuales, config)
        .then(function (response) {
          console.log(response);
         }, function (response) {
         console.log("error..");
         });

      };