1

For context, I'm trying to use XMLHttpRequest in QML to fetch some data from a site. I will then parse that JSON and do whatever is needed, but that's not the issue. Specifically, the issue is that qmlls keeps reporting the [unqualified] error at any mention of XMLHttpRequest. The app and qmllint report no errors at all, I just want qmlls to stop returning that error.

I've tried importing QtQuick 2.15 and QtQml 2.15, but they don't change anything. How do I make qmlls properly detect XMLHttpRequest?

Note: I use the Codium extension "Qt Qml" from Qt, which uses /usr/bin/qmlls6.

A minimal example:

import QtQml 2.15
import QtQuick 2.15

Rectangle {
    function fetch() {
        var request = new XMLHttpRequest()
        request.open('GET', 'https://example.org', true);
        request.onreadystatechange = function() {
            if (request.readyState === XMLHttpRequest.DONE) {
                if (request.status && request.status === 200) {
                    console.log(request.responseText)
                } else {
                    console.log("HTTP:" + request.status + "\nstatustext:" + request.statusText)
                }
            }
        }
        request.send()
    }

    Component.onCompleted: {
        fetch()
    }
}

In this piece of code, XMLHttpRequest() gets underlined with yellow, and hovering over it shows "Unqualified access [unqualified]".

XMLHttpRequest works just fine, returning 200 OK for valid sites.

Oddly enough, using it exactly as the example in the Qt docs works just fine (importing a JS file with the function and calling it from within QML), as suggeested by @smr . Why does this happen? Is this how developers normally use XMLHttpRequest?

5
  • You probably have to reduce the problem to its simplest form, break it down into steps. Does request.status return 200? Is request.responseText correct? If yes, the issue is your external component and not XMLHttpRequest. If no check the server with some tool like Postman, perhaps its response is incorrect. Commented Sep 20 at 8:26
  • Edited the question to answer your comment @folibis Commented Sep 20 at 12:36
  • Consider using it like the example in the documentation. Commented Sep 21 at 4:04
  • You should also mention the qmlls6 warning you get, I think it’s the Unqualified access warning. If that’s the warning you’re seeing, edit your post to include the exact warning text so readers know what to fix. Commented Sep 21 at 4:10
  • @smr Edited the question Commented Sep 21 at 8:05

1 Answer 1

1

Following the sending requests example in the documentation, you can define a helper function outside your QML file and use that function in your QML files. This makes the code reusable, cleaner, easier to use, and also resolves the linter warning.

The code below is taken from the documentation's example.

Scripts.js
.pragma library

function fetch(url, callback = Function.prototype, headers = {}, method = 'GET') {
    const request = new XMLHttpRequest();

    request.open(method, url);

    Object.entries(headers).forEach(([key, value]) => request.setRequestHeader(key, value));

    request.onreadystatechange = () => {
        if(request.readyState === XMLHttpRequest.DONE) {
            callback({
                status: request.status,
                headers: request.getAllResponseHeaders(),
                contentType: request.responseType,
                content: request.response
            });
        }
    }

    request.send();

    return request;
}
Test.qml
Button {
    onClicked: {
        Scripts.fetch('https://example.com', response => print(response.content));
    }
}
CMake
# ...
qt_add_qml_module(
    # ...
    QML_FILES Scripts.js
)

Suppress warnings

Unfortunately, I couldn't find resources explaining why the QML linter shows a warning on XMLHttpRequest, but if you want to use XMLHttpRequest inside QML files you can suppress the warnings using the // qmllint disable unqualified inline comment.
Because the warning is only from the linter and the application runs as expected, you can safely ignore this specific warning.

Button {
    onClicked: {
        const request = new XMLHttpRequest(); // qmllint disable unqualified
        // ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.