[SOLVED]using XMLHttpRequest from imported javascript file produces error(does not when used from QML)



  • This might be obvious to Javascript programmers, but this is a problem for fresh JavaScript programmers.

    The code opens txt file(size is 2MB, but that doesn't matter - might be even bigger - and can be local and can be over network)

    if run from QML this code runs great:
    @

    import "js/Parser.js" as Parser

    ...

    onAccepted:
    {
    var request = new XMLHttpRequest()
    request.open('GET', fileUrl)
    request.onreadystatechange = function(event) {
    if (request.readyState == XMLHttpRequest.DONE)
    {
    txtHello.text = Parser.parseFile(request.responseText)
    }
    }
    request.send()
    }@

    when I create function with duplicate code in Parser.js file:

    @function parseFileHelper(fileUrl)
    {
    var request = new XMLHttpRequest()
    var txt = "";
    request.open('GET', fileUrl)
    request.onreadystatechange = function(event) {
    if (request.readyState == XMLHttpRequest.DONE)
    {
    txt = parseFile(request.responseText)
    }
    }
    request.send()
    return txt
    }
    @

    it fires errors:
    QSslSocket: cannot resolve TLSv1_1_client_method
    QSslSocket: cannot resolve TLSv1_2_client_method
    QSslSocket: cannot resolve TLSv1_1_server_method
    QSslSocket: cannot resolve TLSv1_2_server_method
    QSslSocket: cannot resolve SSL_select_next_proto
    QSslSocket: cannot resolve SSL_CTX_set_next_proto_select_cb
    QSslSocket: cannot resolve SSL_get0_next_proto_negotiated

    What am I doing wrong? The only change is that I want to store all extra JavaScript code in external file - in one place. If there was any information in google or Qt documentation(JavaScript documentation is skipped as it provides too wide information, that doesn't guaranteed to run on QML).

    The problem is that for beauty of code and practical reasons it would be better to keep all XMLHttpRequest usage in external .js file(ok, and getting to understand why there is that QSslSocket error might be a great plus as well) - the problem there is that there will be opened 3-5 files in one session and writing them all in QML is too much of code - all of them can be opened with one function - the different part is with parsing files and that is not a problem to call from external JavaScript file - why should it be with calling XMLHttpRequest?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are missing the OpenSSL libraries. What OS are you running on ?



  • I'm using MS Windows 7 x64 and building for desktop. I don't have OpenSSL installed on my system. I actually need more information on this why I would need to use OpenSSL libraries on opening local text/XML/other file - so far I'm using local file, so I don't think that it is the issue. I'm wondering - are you able to produce positive result by importing JavaScript file? Maybe I'm mising something in code(because it works when it runs from QML - and without OpenSSL).

    Basically I'm opening local txt file with the help of JavaScript. And I'm aware, that I could open files with C++, but I would like to stick to JavaScript for compability reasons on multiple platforms.
    I understand, that XMLHttpRequest has restrictions in Qt due to security issues, but it works when I'm opening file from code, that is written in QML file. If I include the same code in file, that I import it into QML file, it fires errors. The question is why? Is it actually, that XMLHttpRequest is allowed only from QML - not from JavaScript imported file?

    Also I have modified JavaScript code to return text - though it doesn't work, when I process file textual information in Javascript code as well...



  • Ok, it seems, that suggestion by SGaist about installing OpenSSL was a solution(since Qt Creator in Windows supports only 32-bit projects, I installed 32-bit OpenSSL version), but I would like to know the mechanics why JavaScript from imported file requires it, but QML don't.

    Thanks, SGaist - I'm setting it as answered, as it at least solves my problem with importing JavaScript file and solves other logical coding problems.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.