Please nominate your Qt Champions for 2021!

First XHR request very slow.

  • I have a QML page that makes an XHR request to an external server. Right now the server is running on my local machine and the first time this request is made it takes ~1.5 seconds. Each subsequent request is under 100ms.

    When I make this same request using a browser or a script I get a response in under 10ms, so I know the problem isn't there.

    Here is the offending code. Any ideas?

    Edit: the problem isn't with handleResponse() function, I've already tried replacing it with a console.log("response"), and it still takes just as long.

    function login(key) {
    var xhr = new XMLHttpRequest();
    var params = "Fob_num=" + key;"POST","http://localhost:9000/customer_login",true);

    // Send the proper header information along with the request
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", params.length);
    xhr.setRequestHeader("Connection", "close");
    xhr.onreadystatechange = function() {
        if ( xhr.readyState == xhr.DONE) {
            if ( xhr.status == 200) {
            } else {
                console.log("error with login--status:  " + xhr.status)
                displayErr("Oops, something's wrong. Please try again.")


  • Hi,

    I don't know what would cause this, to be honest, but some things which might help your debugging:

    You can provide a QNetworkManager to the QML engine, which will be used for all network communication (including remote imports, remote image fetches, and xmlhttprequests). The API is a bit strange, IIRC, as you actually pass a pointer to a factory function, I think. If your factory instantiates an instance of a custom subclass of QNAM, you can override the performRequest() virtual function (or something, I can't remember). Thus, you can get timings of when the actual network request is performed, compared to when it finishes.

    The issue could be related to QNAM / QtBearer trying to figure out which route to use for the requests, or something, for example.

    You could also try running the application under QtCreator with the QML analyzer enabled, to try to see what's going on, but I expect it's not anything in QML itself which is taking up the time (although I may be wrong).


Log in to reply