Problems receiving xml by request ajax jquery [closed]

1

I am doing tests for an application. I want to make an ajax request with jquery to a php file and receive an xml structure from it.

The request with Jquery I do so:

  $.post({
        url: 'php/procesar_xml.php',
        dataType: 'xml',
    })
    .done(respuesta)
    .fail(error);

function respuesta(datos) {
    console.log(datos);
}

That a simple request that does not send data or anything. I just want to receive the xml structure. I have the php file defined as follows:

  <?php
  header('Content-Type:text/xml');
  $xml='<?xml version="1.0"?>';
  $xml.='<TRABAJADOR>';
  $xml.='<ID_TRABAJADOR>123456</ID_TRABAJADOR>';
  $xml.='<NOMBRE_TRABAJADOR>juan</NOMBRE_TRABAJADOR>';
  $xml.='</TRABAJADOR>';

The request returns the following error message:

Mensaje de error Error: Invalid XML: 
<?xml version="1.0"?><TRABAJADOR><ID_TRABAJADOR>123456</ID_TRABAJADOR><NOMBRE_TRABAJADOR>juan</NOMBRE_TRABAJADOR></TRABAJADOR>

I have tried several ways but it always gives me the same error. What can you owe? Greetings.

    
asked by 18.04.2018 в 13:58
source

1 answer

1

The problem is because the XML lacks a root element .

Solution:

You must add an element that contains all the worker's data.

Example:

<?php
header('Content-Type:text/xml');

$xml='<?xml version="1.0"?>';
$xml.='<TRABAJADOR>';
$xml.='<ID_TRABAJADOR>123456</ID_TRABAJADOR>';
$xml.='<NOMBRE_TRABAJADOR>juan</NOMBRE_TRABAJADOR>';
$xml.='</TRABAJADOR>';
echo $xml;

More Info:

answered by 18.04.2018 в 14:16