Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 0 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;
            }
        }
        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.

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 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? –  londumas Jun 26 '13 at 14:08
show 3 more comments

Your Answer

 
discard

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.