Using QML the send post XML-requests
-
Hi,
I'm trying to write a small QML/JavaScript app. For this, I wanted to send XML-requests to an online API.
How could I send the following XML-request vis post-method from QML?@
<?xml version="1.0" encoding="UTF-8" ?>
<CreateSessionRequest >
<accesskey>Your API-Key</accesskey>
<vendor>Your Vendor-ID/Scout-ID</vendor>
</CreateSessionRequest>
@And how could I parse the XML-response and (potentially) convert it into a json-like javascript object?
thanks!
/tom -
Hi,
You should be able to do this with "XMLHttpRequest":http://doc.qt.nokia.com/4.7/qdeclarativeglobalobject.html#xmlhttprequest
Regards,
Michael -
I have been checking the way XmlHttpRequest works and here is the code that is sending an xml request to remote server:
@onClicked: {
var doc = new XMLHttpRequest();
var postData = "xml=<common><version>1.0</version></common>";
doc.open("POST", "http://api.service.net/api", true);
doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
doc.onreadystatechange = function() {
if (doc.readyState == doc.DONE) {
console.log("Your data has been sent");
}
if (doc.status == 200, doc.readyState == 4) {
var resp = doc.responseXML
console.log("Doc XML response ready")
}
else console.log("hmmm?")
}
doc.send(postData);
}@Now basically I should be able to parse the response using XmlListModel? but how do I use that?
If I put the XmlListModel after the code above it will be outside of javascript and won't work...Could someone advise me?And ofc we will make the pose SOLVED together as I answered (I guess) - 50% of the original question;))
-
@var doc = new XMLHttpRequest();
var postData = "<common><version>1.0</version><api_key>XXX==</api_key></common>";
doc.open("POST", "http://api.service.net/api", true)
doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
doc.send(postData)
doc.onreadystatechange = function() {
if (doc.readyState == 4) {
console.log("Your data has been sent")
if (doc.status == 200) {console.log("URL exists")}}
else console.log("hmm")@My digging continues - as the server replies with 200 I guess I should be getting the reply now. But it takes super brain to understand for me (I am not a programmer;( ) what is the next step to parse it...as I need the data to be available to my application seems like I need to stick to XmlHttpRequest and try to use its parsing capabilities? but then is there a method to make the value extracted - available to the app?
-
I almost give up now, but some progress done...I learned the way ajax works and hence tried to apply the logic. I used russian Yandex.locator api for testing (like Google latitude)...
So I am able to actually get response in xml and browse through the nodes, but whatever I do nodeValue is not working for me returning "undefinded" - BUG?
@onClicked: {
var doc = new XMLHttpRequest();
var postData = "xml=<ya_lbs_request><common><version>1.0</version><api_key>AN8H5E0BAAAAz4biJAUARzyc-Zh1KKgasTzFatJuqzsT-xkAAAAAAAAAAACUROPR814LedbZKMmgp84RFNv2TQ==</api_key></common><gsm_cells><cell><countrycode>250</countrycode><operatorid>1</operatorid><cellid>2294</cellid><lac>6405</lac><signal_strength>-45</signal_strength><age>1000</age></cell></gsm_cells></ya_lbs_request>";
doc.open("POST", "http://api.lbs.yandex.net/geolocation", true)
doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
doc.send(postData)
doc.onreadystatechange = function() {
if (doc.readyState == 4) {
console.log("Your data has been sent")
if (doc.status == 200) {console.log("URL exists");
var root = doc.responseXML.documentElement;
console.log(root.childNodes[1].childNodes[1].name)
}}
else console.log("hmm")
}
}@ -
"link":http://www.w3schools.com/dom/dom_nodes_get.asp
works now)
So response I am parsing is as follows:
@<?xml version="1.0" encoding="UTF-8"?>
<ya_lbs_response>
<position>
<latitude>55.7351526</latitude>
<longitude>37.5951196</longitude>
<altitude>0.0</altitude>
<precision>826.319232</precision>
<altitude_precision>30.0</altitude_precision>
<type>gsm</type>
</position>
</ya_lbs_response>@@{
var doc = new XMLHttpRequest();
var postData = "xml=<ya_lbs_request><common><version>1.0</version><api_key>AN8H5E0BAAAAz4biJAUARzyc-Zh1KKgasTzFatJuqzsT-xkAAAAAAAAAAACUROPR814LedbZKMmgp84RFNv2TQ==</api_key></common><gsm_cells><cell><countrycode>250</countrycode><operatorid>1</operatorid><cellid>2294</cellid><lac>6405</lac><signal_strength>-45</signal_strength><age>1000</age></cell></gsm_cells></ya_lbs_request>";
doc.open("POST", "http://api.lbs.yandex.net/geolocation", true)
doc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
doc.send(postData)
doc.onreadystatechange = function() {
if (doc.readyState == 4) {
console.log("Your data has been sent")
if (doc.status == 200) {console.log("URL exists");
var root = doc.responseXML.documentElement;
console.log(root.childNodes[1].childNodes[1].childNodes[0].nodeValue)
}}
else console.log("hmm")
}
}@Worth reading this: "LINK":http://www.w3schools.com/dom/dom_nodes_get.asp