Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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: 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 = "" + 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:

Thank you if you know the answer. It is the last piece missing to finish my Ubuntu touch app.

share|improve this question
up vote 1 down vote accepted

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;
        }"get", url);
        doc.setRequestHeader("Content-Encoding", "UTF-8");

    Text {
        id: mainText
        anchors.centerIn: parent
        text: "Click Me";

    MouseArea {
        anchors.fill: parent
        onClicked: {

When dealing with asynchronous requests you should keep in mind that you won't get results instantly.

share|improve this answer
Thank you for the answer. How can I get the non ascii characters? All of them are replaced by a diamon with a question mark. – londumas Jun 25 '13 at 7:28
@londumas try to declare encoding in your html output header to UTF-8. – xeranas Jun 25 '13 at 11:11
I 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 "è". – londumas Jun 26 '13 at 4:25
@londumas sorry, I misunderstand your situation. I add doc.setRequestHeader("Content-Encoding", "UTF-8"); line to my answer. – xeranas Jun 26 '13 at 11:11
Thank you @xeranas, I found the problem. It was the difference between this url ""; and this one "";. 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]. – londumas Jun 27 '13 at 2:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.