get text from a Website in javascript/qml
XMLHttpRequest is definitely one of way to solve this issue. Here my quick sample:
import QtQuick 2.0
Rectangle {
width: 360
height: 360
function setText(url) {
var doc = new XMLHttpRequest();
doc.onreadystatechange = function() {
if (doc.readyState == XMLHttpRequest.DONE) {
mainText.text = doc.responseText;
}
}
doc.open("get", url);
doc.setRequestHeader("Content-Encoding", "UTF-8");
doc.send();
}
Text {
id: mainText
anchors.centerIn: parent
text: "Click Me";
}
MouseArea {
anchors.fill: parent
onClicked: {
setText("http://feed.evangelizo.org/reader.php?date=20130616&type=liturgic_t&lang=AM&content=GSP");
}
}
}
When dealing with asynchronous requests you should keep in mind that you won't get results instantly.
Related videos on Youtube
londumas
Updated on September 18, 2022Comments
-
londumas almost 2 years
I have this Website I would like to copy the text from. I don't know much but it looks like it uses php (here is the site: http://feed.evangelizo.org/reader.php) So I would like to write a fuction that would return the text inside a site.
something like this:
function example() { var currentTime = new Date(); var month = currentTime.getMonth() + 1; var day = currentTime.getDate(); var year = currentTime.getFullYear(); if (day < 10) day = '0'+day; if (month < 10) month = '0'+month; var httpWeb = "http://feed.evangelizo.org/reader.php?date=" + year + month + day + "&type=reading&lang=FR&content=GSP"; return getText(httpWeb); }
The thing is to write this getText(string) function. How can I do that in javascript/qml? I have seen something about XMLHttpRequest but I don't understand it.
Here is an example of text I would like: http://feed.evangelizo.org/reader.php?date=20130616&type=liturgic_t&lang=AM&content=GSP
Thank you if you know the answer. It is the last piece missing to finish my Ubuntu touch app.
-
londumas about 11 yearsThank you for the answer. How can I get the non ascii characters? All of them are replaced by a diamon with a question mark.
-
user9440008 about 11 years@londumas try to declare encoding in your html output header to UTF-8.
-
londumas about 11 yearsI have found setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); is that the good thing? I tried using it but it doesn't seem to work. What is very strange is that on the doc of the website I get the text from they say they use UTF-8 but on a French text it gives that link note the "è" transformed into "è".
-
user9440008 about 11 years@londumas sorry, I misunderstand your situation. I add
doc.setRequestHeader("Content-Encoding", "UTF-8");
line to my answer. -
londumas about 11 yearsThank for the answer. I have tried your new answer. I still have the question marks. Maybe it is linked to the website. Since instead of a "è" there is a "è" when you look at the link above. What do you think?
-
user9440008 about 11 years@londumas I test sample with your French text link and it displayed text correctly (with letter "è"). Reason why you see question marks could be related with your local environment maybe your qml itself is incorrectly encoded (it less likely but worth check it anyway).
-
user9440008 about 11 years@londumas reason why you see "Ã" instead of "è" in browser because encoding is not defined on html header. That's why we need use setRequestHeader.
-
londumas about 11 yearsThank you @xeranas, I found the problem. It was the difference between this url "feed.evangelizo.org/reader.php?" and this one "feed.evangelizo.org/v2/reader.php?". Note the "v2". I think the website made its changed few days ago and I didn't notice. With the good adress, There is no need for the "setRequestHeader". Thank again. I write as [solved].