0

I'm doing a POST request in AJAX, and when I get the response, if I try to alert it for example, I get this:

[object XMLDocument]

here is my code:

$(document).ready(function(){
$("button").click(function(){
    var inputreg = document.getElementById("reginput").value;
    $.post("linkhere...",
    {
      RegistrationNumber: inputreg,
      username: "myusername",
      dataType: "xml"
    },
    function(data){
        alert(data);
    });
}); });

I'm trying to get a certain value from the xml, for example 'Description'..

edit: here is part of the response:

<vehicleData>
<ABICode>12345</ABICode>
<Description>lorem ipsum</Description></vehicleData>
alexcr
  • 171
  • 1
  • 12
  • `alert()` is **not** a debugging tool. Anything you pass to it will be converted to a string (via `Object.prototype.toString()`). Try `console.log(data)` instead (and open your console of course) – Phil Jan 18 '18 at 23:21
  • Also, you're using [`$.post()`](https://api.jquery.com/jquery.post/) incorrectly. The `dataType` should be passed as the 3rd argument, eg `$.post(url, function(data) { ... }, 'xml')`. That is assuming you don't want to actually post `dataType=xml` in the request body – Phil Jan 18 '18 at 23:24
  • Possible duplicate of [How can I display a JavaScript object?](https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object) – Phil Jan 18 '18 at 23:25
  • ok so, if I do console.log(data), and open the console, I get the right xml response, but how can I get just a value from that xml? for example, the Description? – alexcr Jan 18 '18 at 23:28
  • You can use DOM methods to traverse the document, same as you would for the HTML page. For example `data.getElementsByTagName('Description')` – Phil Jan 18 '18 at 23:29
  • ok so, I've tryed to do 'alert(data.getElementsByTagName('Description'));' and I got '[object HTMLCollection]' – alexcr Jan 18 '18 at 23:36
  • Really, `alert()` again? And you wonder why you can't see anything useful :\ – Phil Jan 18 '18 at 23:37
  • ok than, I did console.log instead, and it shows it in my console, but it shows it among different data, and the part I'm interested in is at innerHTML: 'value I'm interested' , I also need to assign that value to a variable.. – alexcr Jan 18 '18 at 23:40
  • Here is part of it: 12345 lorem ipsum – alexcr Jan 18 '18 at 23:48
  • I have edited the question, and added part of the response – alexcr Jan 18 '18 at 23:53

1 Answers1

1

Given the data is an XMLDocument, you can use DOM methods on it to navigate and retrieve values. For example

let description = data.querySelector('Description').textContent

Here's an example

// ignore this section, it's just setting up the XML document
//////////////////////////
const xml = `<vehicleData>
<ABICode>12345</ABICode>
<Description>lorem ipsum</Description></vehicleData>`

const parser = new DOMParser()
const data = parser.parseFromString(xml, 'text/xml')
//////////////////////////

console.info(data.querySelector('Description').textContent)
Phil
  • 157,677
  • 23
  • 242
  • 245
  • I assigned const xml = data; and after did the code you suggested, but I'm getting 'Uncaught TypeError: Cannot read property 'textContent' of null' inside my console.. – alexcr Jan 19 '18 at 00:11
  • @alexcr really not sure why you'd do that. The `xml` in my example is just to emulate the data returned by your AJAX request. The important part is `data.querySelector('Description')`. If that isn't working, then your XML isn't a good example of the _real_ data – Phil Jan 19 '18 at 00:15