Pass xml with ajax to php

2

Hello Good afternoon friends, see if you can help me:

I have a function in jquery that creates a xml in a variable (I do not save it to disk) and that variable I want to pass it via ajax to server php and receive it with method _POST .

The file php gives the following error:

  

Notice : Undefined index: Xml in C: \ xampp \ htdocs \ medialab \ AddReferences.php on line 6

This is my code:

$("#GuardarReferencia").click(function(){ 

       var xmlResult = TableToXml();

       console.dir(xmlResult)
       //alert(xmlResult.XMLSchema())
       $.ajax({ // Envío por Ajax el las Referencias en Formato Xml
            url: "AddReferencias.php",
            type: "post",
            dataType: "text",
            data: { 'Xml': xmlResult },
            success: function(data) {
              alert("Devuelve: "+data)

            },error: function(data) {
              MensajeGritter("Error", "Sucedió un Error Inesperado", "gritter-error");
            }
          });

    });

    function TableToXml(Table){
      var filas = $("#listaPacientesModalOK").find("tr"); 

      var $root = $('<BeCargoReferencia xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /> <Ordenes />');

          for(i=0; i<filas.length; i++){ //Recorre las filas 1 a 1
            var celdas = $(filas[i]).find("td"); //devolverá las celdas de una fila
            Item = $(celdas[26]).text().replace('\n', '').trim();
            Paterno     = $(celdas[5]).text();
            Materno     = $(celdas[6]).text();
            Nombres     = $(celdas[7]).text();
            Documento   = $(celdas[24]).text().replace('\n', '').trim();
            FecNac      = $(celdas[8]).text();
            Edad        = $(celdas[9]).text();
            Sexo        = $(celdas[10]).text();
            Prioridad   = $(celdas[13]).text();
            Observacion = $(celdas[14]).text();

          $.createElement = function(name)
          {
              return $('<'+name+' />');
          };

          $.fn.appendNewElement = function(name)
          {
              this.each(function(i)
              {
                  $(this).append('<'+name+' />');
              });
              return this;
          }

          $root.append
          (


                  $('<BeOrdenReferencia />').append
                  (
                      $('<Item />').text(Item),
                      $('<Paterno />').text(Paterno),
                      $('<Materno />').text(Materno),
                      $('<Nombres />').text(Nombres),
                      $('<Documento />').text(Documento),
                      $('<FecNac />').text(FecNac),
                      $('<Edad />').text(Edad),
                      $('<Sexo />').text(Sexo.substr(0, 1)),
                      $('<Prioridad />').text(Prioridad),
                      $('<Observacion />').text(Observacion)

                  )
                   .appendNewElement('Detalle')



          );

          var $referencias = $root.find('BeOrdenReferencia');

          var $analisis = $referencias.find('Detalle');

          var filasNew = $("#listaAnalisisModalOK").find("tr"); 

          for(y=0; y<filasNew.length; y++){ //Recorre las filas 1 a 1
              var campo = $(filasNew[y]).find("td"); //devolverá las celdas de una fila
              ItemDet = $(campo[1]).text().replace('\n', '').trim();
              CodigoDet     = $(campo[2]).text().replace('\n', '').trim();
              NombreDet     = $(campo[3]).text().replace('\n', '').trim();
              PrecioDet     = $(campo[4]).text().replace('\n', '').trim();
              Cod_tarDet    = $(campo[5]).text().replace('\n', '').trim();
              Cod_perDet    = $(campo[6]).text().replace('\n', '').trim();

              if(ItemDet == Item){
                  var $newAnalisis = $.createElement('BeDetalle');
                  $newAnalisis.append($('<Item />').text(Item));
                  $newAnalisis.append($.createElement('Codigo').text(CodigoDet));
                  $newAnalisis.append($.createElement('Nombre').text(NombreDet));
                  $newAnalisis.append($.createElement('Precio').text(PrecioDet));
                  $newAnalisis.append($.createElement('Cod_tar').text(Cod_tarDet));
                  $newAnalisis.append($.createElement('Cod_per').text(Cod_perDet));
                  $analisis.append($newAnalisis);
              }

          }

        }

    return $root;
    }
    
asked by Alexander J. Marcano 04.05.2018 в 20:30
source

1 answer

1

The variable xmlResult can be treated like any other jQuery object. You can do what you want like this:

$.ajax({ // Envío por Ajax el las Referencias en Formato Xml
        url: "AddReferencias.php",
        type: "post",
        dataType: "text",
        data: { 'Xml': xmlResult[0].outerHTML },
        success: function(data) {
          alert("Devuelve: "+data)

        },error: function(data) {
          MensajeGritter("Error", "Sucedió un Error Inesperado", "gritter-error");
        }
      });
    
answered by 04.05.2018 / 21:17
source