Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript: xmlhttprequest request and server answer
    primarykey
    data
    text
    <p>I'm starting off with js. I want to fetch data from a server with xml. I wonder how to send a request as, and get an anser in xml through javascript functions. It says I need a POST-Request and send an xml in the form:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;ft&gt; &lt;request clientId="123" apiName="api_search_location_stops_nearby" apiVersion="2.0"&gt; &lt;client clientId="123"/&gt; &lt;requestType&gt;api_search_location_stops_nearby&lt;/requestType&gt; &lt;outputCoords&gt;WGS84&lt;/outputCoords&gt; &lt;fromCoordName&gt;WGS84&lt;/fromCoordName&gt; &lt;fromType&gt;coords&lt;/fromType&gt; &lt;fromWgs84Lat&gt;48.22&lt;/fromWgs84Lat&gt; &lt;fromWgs84Lon&gt;16.39&lt;/fromWgs84Lon&gt; &lt;/request&gt; &lt;/ft&gt; </code></pre> <p>To then get an xml answer. It has 2 or 3 nodes in it, which I'm interested in. From there on, it'll be no big deal.</p> <p>It is all about a strange API from the vienna public transport company: <a href="http://akirk.github.io/Wiener-Linien-API/" rel="nofollow">http://akirk.github.io/Wiener-Linien-API/</a> I basically want to get (open)data from them.</p> <p>Here: <a href="https://techscreen.tuwien.ac.at/node/794" rel="nofollow">https://techscreen.tuwien.ac.at/node/794</a> I found a solution for php.. </p> <p>My try:</p> <pre><code> // Bare bones XML writer - no attributes function xmlElement(name,content){ var xml if (!content){ xml = '&lt;' + name + '&gt;' + '&lt;/' + name + '&gt;' } else { xml = '&lt;'+ name + '&gt;' + content + '&lt;/' + name + '&gt;' } return xml } function sendRequest() { var xmlReq xmlReq = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;"; xmlReq = xmlReq + "&lt;ft&gt;"; xmlReq = xmlReq + "&lt;request clientId=\"123\" apiName=\"api_get_monitor\" apiVersion=\"2.0\"&gt;"; xmlReq = xmlReq + "&lt;client clientId=\"123\"/&gt;"; xmlReq = xmlReq + xmlElement("requestType", "api_get_monitor"); xmlReq = xmlReq + xmlElement("monitor", xmlElement("outputCoords", "WGS84") + xmlElement("type","stop") + xmlElement("name","60201040") + xmlElement("year","2013") + xmlElement("month","10") + xmlElement("day","3") + xmlElement("hour","8") + xmlElement("minute","0") + xmlElement("line") + xmlElement("sourceFrom","stoplist") ); xmlReq = xmlReq + "&lt;/request&gt;" + "&lt;/ft&gt;"; text1.text = xmlReq; var xhr = new XMLHttpRequest(); xhr.onload = handleRequest; xhr.open("POST", "http://webservice.qando.at/2.0/webservice.ft"); // POST or GET xhr.send(xmlReq); // xhr.responseXML // this is allways null } function handleRequest(answer) { console.log(answer.responseType); console.log(answer.responseXML); } </code></pre> <p>The core points of my question: On my code, should there be GET or POST? Is the request built up to fit the style above (or do I need linebreaks or convert to a DOM xml thing)? How does the recieving thing work. Am I doing this right? Should the variable answer then contain the xml with the answer?</p> <p>This code is somehow not working. I printed the xml-string to the console and it looks just like above (without linebreaks). But the handleRequest function doesn't print anything (it is not called at all).</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload