Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED]using XMLHttpRequest from imported javascript file produces error(does not when used from QML)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glicanear
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glicanear
        wrote on last edited by
        #3

        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...

        1 Reply Last reply
        0
        • G Offline
          G Offline
          glicanear
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved