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. WebEngineView cannot load external js from http
Forum Updated to NodeBB v4.3 + New Features

WebEngineView cannot load external js from http

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 287 Views
  • 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.
  • C Offline
    C Offline
    charlie2024
    wrote on 10 Apr 2024, 17:37 last edited by
    #1

    Hi! So I want to embed a map in my application using WebEngineView. Here is my html and QML code:

    check.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Test Mapbox GL JS Load</title>
        <link href="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.css" rel="stylesheet">
        <script src="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.js" onerror="console.error('Import failure')"></script>
    </head>
    <body>
        <h1>Testing Mapbox GL JS Load</h1>
        <div id="result"></div>
    
        <script>
            function checkMapboxGLJS() {
                const resultElement = document.getElementById('result');
                if (window.mapboxgl) {
                    resultElement.innerHTML = "Mapbox GL JS is loaded successfully.";
                    console.log("Mapbox GL JS is loaded successfully.");
                } else {
                    resultElement.innerHTML = "Failed to load Mapbox GL JS.";
                    console.error("Failed to load Mapbox GL JS.");
                }
            }
    
            // Ensure the check runs after the library should have loaded
            window.onload = checkMapboxGLJS;
        </script>
    </body>
    </html>
    

    mapview.qml

    import QtQuick 2.15
    import QtWebEngine 1.8
    
    import "../../shared"
    
    Item {
        anchors.fill: parent
        anchors.centerIn: parent
        Text {
            text: "Map"
            anchors.centerIn: parent
            font.pixelSize: 60
        }
    
        WebEngineView {
            anchors.fill: parent
            url: Qt.resolvedUrl("./check.html") // Adjust the path to where your HTML file is located
            // url: "http://google.com"
    
            onLoadingChanged: function(loadingInfo) {
                // if (loadingInfo.status == WebEngineView.LoadFailedStatus) {
                console.log("Load info: " + loadingInfo.errorString);
                // }
            }
        }
    }
    

    When running these scripts, I got qml: Load info: The webpage at <strong jscontent="failedUrl"></strong> might be temporarily down or it may have moved permanently to a new web address.

    I have tried moving the loading part into QML as well, and also got an import error. It's frustrating but I cannot get more detailed information on why the import failed.

    This HTML file can be loaded in Chrome, and my QML app can also access http://google.com, so I think it's some kind of permission issue. Weirdly I can't seem to find the exact solution online either, although I thought this should be a pretty common problem.

    Any help is appreciated!

    C 1 Reply Last reply 17 Apr 2024, 01:19
    0
    • C charlie2024
      10 Apr 2024, 17:37

      Hi! So I want to embed a map in my application using WebEngineView. Here is my html and QML code:

      check.html

      <!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8">
          <title>Test Mapbox GL JS Load</title>
          <link href="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.css" rel="stylesheet">
          <script src="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.js" onerror="console.error('Import failure')"></script>
      </head>
      <body>
          <h1>Testing Mapbox GL JS Load</h1>
          <div id="result"></div>
      
          <script>
              function checkMapboxGLJS() {
                  const resultElement = document.getElementById('result');
                  if (window.mapboxgl) {
                      resultElement.innerHTML = "Mapbox GL JS is loaded successfully.";
                      console.log("Mapbox GL JS is loaded successfully.");
                  } else {
                      resultElement.innerHTML = "Failed to load Mapbox GL JS.";
                      console.error("Failed to load Mapbox GL JS.");
                  }
              }
      
              // Ensure the check runs after the library should have loaded
              window.onload = checkMapboxGLJS;
          </script>
      </body>
      </html>
      

      mapview.qml

      import QtQuick 2.15
      import QtWebEngine 1.8
      
      import "../../shared"
      
      Item {
          anchors.fill: parent
          anchors.centerIn: parent
          Text {
              text: "Map"
              anchors.centerIn: parent
              font.pixelSize: 60
          }
      
          WebEngineView {
              anchors.fill: parent
              url: Qt.resolvedUrl("./check.html") // Adjust the path to where your HTML file is located
              // url: "http://google.com"
      
              onLoadingChanged: function(loadingInfo) {
                  // if (loadingInfo.status == WebEngineView.LoadFailedStatus) {
                  console.log("Load info: " + loadingInfo.errorString);
                  // }
              }
          }
      }
      

      When running these scripts, I got qml: Load info: The webpage at <strong jscontent="failedUrl"></strong> might be temporarily down or it may have moved permanently to a new web address.

      I have tried moving the loading part into QML as well, and also got an import error. It's frustrating but I cannot get more detailed information on why the import failed.

      This HTML file can be loaded in Chrome, and my QML app can also access http://google.com, so I think it's some kind of permission issue. Weirdly I can't seem to find the exact solution online either, although I thought this should be a pretty common problem.

      Any help is appreciated!

      C Offline
      C Offline
      charlie2024
      wrote on 17 Apr 2024, 01:19 last edited by
      #2

      @charlie2024 Friends! I fixed this. Turned out we need to add this line to WebEngineView.

              settings {
                  localContentCanAccessRemoteUrls: true
              }
      

      This is the problem: https://www.telerik.com/blogs/all-you-need-to-know-cors-errors

      1 Reply Last reply
      0
      • C charlie2024 has marked this topic as solved on 17 Apr 2024, 01:19

      1/2

      10 Apr 2024, 17:37

      • Login

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