Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

XMLHttpRequest - limitation receiving data asynchronously



  • Hello everybody.

    I'm developing a mobile QtQuick app but I encountered a general problem with the XMLHttpRequest-object:
    The app communicates with a server via a JSON API (sending JSON objects and receiving JSON objects as response).
    I use the XMLHttpRequest for asynchronous requests. There is one API call that makes the server respond with a JSON object which contains about ~500 data records (< 40 kilobytes). I can't manage to receive this response object. I checked that the server sends valid JSON data.
    Do I have to modify the HTTP headers for the app's requests?
    The mentioned response object is received successfully when the app requests the data synchronously.

    I shortened the JavaScript-method which I use to request data. The second parameter of the function is supposed to be a function to allow processing of the received data.

    function test(jsonRequest, callBack)
    {
        var http = new XMLHttpRequest()
        var url = "http://example.com"
        var async = true
        http.open("POST", url, async)
        // send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        http.setRequestHeader("Content-length", jsonRequest.length)
        http.setRequestHeader("Connection", "Close")
        http.onreadystatechange = function()
        {
            if (http.readyState === XMLHttpRequest.DONE)
            {
                if (http.status === 200)
                {
                    var jsonResponse = http.responseText
                    try
                    {
                        if (jsonResponse == "")
                        {
                            throw "API-Error"
                        }
                        var jsonResponseObject = JSON.parse(jsonResponse)
                        if (typeof(callBack) == "function")
                            callBack(jsonResponseObject)
                        else
                            throw "no callBack-function specified: type of callBack: " + typeof(callBack)
                    }
                    catch(e)
                    {
                        if (Qt.platform.os == "windows")
                            console.log("\n\n" + e + ":\njsonRequest: >" + jsonRequest + "<\njsonResponse: >" + jsonResponse + "<\n")
                    }
                }
            }
        }
        http.send(jsonRequest)
    }
    

    Thank you for reading.


  • Moderators

    @BjoernK
    such a feature is not part of the HTTP standard.
    Is the server application under your control? If so you need to add a custom parameter to the request to define the response limit.



  • @raven-worx
    Thank you for your answer.
    The server is not directly under my control but I can send suggestions to the person in charge. I will suggest to modify the API so one can specify the desired amount of data records per call.


Log in to reply