How to handle xml return in Jquery

Asked

Viewed 574 times

0

I am making an Ajax request for a PHP page that returns several data in XML. If I make a simple code recovery by opening the PHP page directly, sure, however I am making the request for another page that calls the data from another PHP via Ajax with Jquery.

My job today:

$.ajax({
    url: "consultaPlaca.php",
    type: "get",
    data: {placa : 'XXX9999'},
    success: function (retorno) {
teste = retorno;
    console.log(retorno.Placa.text());
      localStorage.setItem('placa', $("placa").text());
      localStorage.setItem('renavam', $("renavam").text());
      localStorage.setItem('tipo', $("tipo").text());
      localStorage.setItem('marca', $("marca").text());
      localStorage.setItem('modelo', $("modelo").text());
      localStorage.setItem('importado', $("importado").text());
      localStorage.setItem('combustivel', $("combustivel").text());
      localStorage.setItem('cor', $("cor").text());
      localStorage.setItem('categoria', $("categoria").text());
      localStorage.setItem('roubofurto', $("roubofurto").text());
      localStorage.setItem('roubofurtocodigo', $("roubofurto roubofurtocodigo").text());
      localStorage.setItem('roubofurtodescricao', $("roubofurto roubofurtodescricao").text());
      localStorage.setItem('roubofurtocomplemento', $("roubofurto roubofurtocomplemento").text());
      localStorage.setItem('anofabricacao', $("anofabricacao").text());
      localStorage.setItem('anolicenciamento', $("anolicenciamento").text());
      localStorage.setItem('anomodelo', $("anomodelo").text());
      localStorage.setItem('ipva', $("ipva").text());
      localStorage.setItem('inspecaoveicular', $("inspecaoveicular").text());
      localStorage.setItem('multascetesb', $("multascetesb").text());
      localStorage.setItem('multasder', $("multasder").text());
      localStorage.setItem('multasdersa', $("multasdersa").text());
      localStorage.setItem('multasdetran', $("multasdetran").text());
      localStorage.setItem('multasmunicipais', $("multasmunicipais").text());
      localStorage.setItem('multasprf', $("multasprf").text());
      localStorage.setItem('observacoes', $("observacoes").text());
      localStorage.setItem('registroguincho', $("registroguincho").text());
      localStorage.setItem('restricaoadministrativa', $("restricaoadministrativa").text());
      localStorage.setItem('restricaofinanceira', $("restricaofinanceira").text());
      localStorage.setItem('restricaojudiciaria', $("restricaojudiciaria").text());
      localStorage.setItem('restricaotributaria', $("restricaotributaria").text());
      localStorage.setItem('totalmultas', $("totalmultas").text());
      localStorage.setItem('totalimpostos', $("totalimpostos").text());
      localStorage.setItem('total', $("total").text());
      localStorage.setItem('datahora', $("datahora").text());
      localStorage.setItem('status', $("transacao status").text());
      localStorage.setItem('codigostatus', $("transacao codigostatus").text());
      localStorage.setItem('codigostatusdescricao', $("transacao codigostatusdescricao").text());      
    }
  });

This works if I play directly on the console of the page that makes the query, however via get from AJAX, comes the data, but I can’t catch them doing just that: retorno.placa.text();

  • Edith the question and put the code PHP.

  • You can show an excerpt from this xml?

  • 1

    I think you should wear $(retorno).find("placa").text() and so on. In this way $("placa").text() jQuery does not know the element $("placa").

  • If the data comes in xml it is necessary to specify dataType: "xml" on request or make parse of the response before using it

1 answer

0

It is necessary to specify dataType: "xml" in Ajax to recognize that it is an XML structure:

$.ajax({
    url: "conecta.php",
    type: "get",
    data: {placa : 'XXX9999'},
    dataType: "xml",
...

Then take the return values according to the XML structure. Suppose your XML returns the structure below, which will be in the variable retorno of your Ajax code:

<infos>
   <placa>abc-10203</placa>
   <marca>volks</marca>
    <transacao>
        <status>ok</status>
    </transacao>
</infos>

To get every piece of information, you need to use the structure:

$(retorno).find("placa").text(); // pego a placa
$(retorno).find("marca").text(); // pego a marca
$(retorno).find("status").text(); // pego o status dentro da tag "transacao"
// e assim por diante

Your code would look like this:

$.ajax({
    url: "consultaPlaca.php",
    type: "get",
    data: {placa : 'XXX9999'},
    dataType: "xml",
    success: function (retorno) {
teste = retorno;
    console.log($(retorno).find("placa").text());
      localStorage.setItem('placa', $(retorno).find("placa").text());
      localStorage.setItem('renavam', $(retorno).find("renavam").text());
      localStorage.setItem('tipo', $(retorno).find("tipo").text());
      localStorage.setItem('marca', $(retorno).find("marca").text());
      localStorage.setItem('modelo', $(retorno).find("modelo").text());
      localStorage.setItem('importado', $(retorno).find("importado").text());
      localStorage.setItem('combustivel', $(retorno).find("combustivel").text());
      localStorage.setItem('cor', $(retorno).find("cor").text());
      localStorage.setItem('categoria', $(retorno).find("categoria").text());
      localStorage.setItem('roubofurto', $(retorno).find("roubofurto").text());
      localStorage.setItem('roubofurtocodigo', $(retorno).find("roubofurtocodigo").text());
      localStorage.setItem('roubofurtodescricao', $(retorno).find("roubofurtodescricao").text());
      localStorage.setItem('roubofurtocomplemento', $(retorno).find("roubofurtocomplemento").text());
      localStorage.setItem('anofabricacao', $(retorno).find("anofabricacao").text());
      localStorage.setItem('anolicenciamento', $(retorno).find("anolicenciamento").text());
      localStorage.setItem('anomodelo', $(retorno).find("anomodelo").text());
      localStorage.setItem('ipva', $(retorno).find("ipva").text());
      localStorage.setItem('inspecaoveicular', $(retorno).find("inspecaoveicular").text());
      localStorage.setItem('multascetesb', $(retorno).find("multascetesb").text());
      localStorage.setItem('multasder', $(retorno).find("multasder").text());
      localStorage.setItem('multasdersa', $(retorno).find("multasdersa").text());
      localStorage.setItem('multasdetran', $(retorno).find("multasdetran").text());
      localStorage.setItem('multasmunicipais', $(retorno).find("multasmunicipais").text());
      localStorage.setItem('multasprf', $(retorno).find("multasprf").text());
      localStorage.setItem('observacoes', $(retorno).find("observacoes").text());
      localStorage.setItem('registroguincho', $(retorno).find("registroguincho").text());
      localStorage.setItem('restricaoadministrativa', $(retorno).find("restricaoadministrativa").text());
      localStorage.setItem('restricaofinanceira', $(retorno).find("restricaofinanceira").text());
      localStorage.setItem('restricaojudiciaria', $(retorno).find("restricaojudiciaria").text());
      localStorage.setItem('restricaotributaria', $(retorno).find("restricaotributaria").text());
      localStorage.setItem('totalmultas', $(retorno).find("totalmultas").text());
      localStorage.setItem('totalimpostos', $(retorno).find("totalimpostos").text());
      localStorage.setItem('total', $(retorno).find("total").text());
      localStorage.setItem('datahora', $(retorno).find("datahora").text());
      localStorage.setItem('status', $(retorno).find("status").text());
      localStorage.setItem('codigostatus', $(retorno).find("codigostatus").text());
      localStorage.setItem('codigostatusdescricao', $(retorno).find("codigostatusdescricao").text());      
    }
  });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.