Loading remote content on a server for my Firefox OS application - On the Web and FxOS device

I haven't come across this issue in any forum, so I decided to reach out here.

I'm encountering a problem with my FirefoxOS app on my FirefoxOS device (Geeksphone Developer Preview) when trying to retrieve remote content from the server.

I am making AJAX calls to a PHP file on the server from my JS files, and successfully displaying the content result in my HTML files. Everything seems to be working fine.

Below is the content of my javascript file

window.addEventListener('DOMContentLoaded', function() {
    'use strict';
    var translate = navigator.mozL10n.get;
    navigator.mozL10n.once(start);
    function start() {
        var message = document.getElementById('message');
        message.textContent = translate('message');
        show_content();
    }
    document.getElementById("btn_1").addEventListener("click",function(){
        console.log("hola mundo");
        alert("hola mundo");
    });

    document.getElementById("btn_2").addEventListener("click",function(){
        console.log("adios mundo");
        alert("adios mundo");
    });

    function show_content(){
        var xmlhttp = new XMLHttpRequest();
        var url = server_path + "ejemplo.php";
        xmlhttp.open("GET", url, true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txt_server").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.send();
    }

});

function aviso(){
    console.log("clicked");
    alert("clicked");
}

Here is the content of my HTML file

<html>
      <head>
            <meta charset="utf-8">
            <title>Example app</title>
            <meta name="description" content="A privileged app stub">
            <meta name="viewport" content="width=device-width">
            <link rel="stylesheet" href="css/app.css">
            <script type="text/javascript" src="js/app.js" defer></script>
            <link rel="prefetch" type="application/l10n" href="data/locales.ini" />
            <script type="text/javascript" src="js/libs/l10n.js" defer></script>
        </head>
      <body>
            <section>
                <h1 data-l10n-id="app_title">My example app</h1>
                <p data-l10n-id="app_description">With this example, I want to show a button loaded in HTML and then load content from server by XMLHttpRequest. Two buttons will have click function developed, with alert and console message.</p>
                <p id="message"></p>
                <button class="wide" data-l10n-id="btn_1" id="btn_1">Click me 1</button>
                <button class="wide" data-l10n-id="btn_2" id="btn_2" hidden></button>
                <p><span id="txt_server"></span></p>
            </section>
        </body>
    </html>

This is the content of my PHP file

<?php
    //to solve CORS advice
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With,Content-Type, Accept");
    header("Content-Type: text/html;charset=utf-8");    

    echo "<button class=\"wide\" data-l10n-id=\"btn_2\" id=\"btn_2\">Click me 2</button><br/>";
    echo "<a onclick=\"aviso();\" class=\"wide\">Click me 3</a>";

?>

The issues I am facing are:

  1. The dynamically created button from the PHP file has no clickable function, neither on the web nor in the FxOS app.
  2. The "a" element also created in the PHP file has a clickable function on the web [showing an alert and log], but it does not work within the FxOS device app.

You can test what I am explaining at this link. If you wish to view the code, there is a zip file available for download here.

Thank you for taking the time to read this. Feel free to ask if you need clarification on anything.

Answer №1

It appears that there has been a CSP violation, which can be common when trying to use onclick events. More information about CSP violations can be found at this link.

Answer №2

As part of the CORS Spec Section 7.3, it is intended that certain restrictions are in place:

  • HTTPS connections are not permitted due to certificate errors

If you encounter issues with self-signed certificates, consider utilizing systemXHR client-side instead of implementing CORS server-side by following this guide: here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain the date in the following format: 2016-01-01T00:00:00.000-00:00

Can someone help me convert this date to the correct format for the mercadolibre api? I need it to be like this: 2016-01-01T00:00:00.000-00:00 However, when I try with the following code: var date_from = new Date(); date_from.setDate(date_from.getDa ...

Import data from a local file using SQL

Here is an example of how my data appears: company_name,brand_name,product_name,usage_annotation,organic_standard ASB Greenworld Ltd.,ASB Greenworld Ltd.,Organic Garden Manure,,"COS,NOP" ASB Greenworld Ltd.,ASB Greenworld Ltd.,Organic Seed & Herb Soil ...

Organize intricate JavaScript Arrays

Similar Question: How to sort an array of javascript objects? I'm in the process of transitioning some of my older ActionScript 2.0 code to JavaScript. While most things are running smoothly, I've hit a roadblock when trying to numerically s ...

Ways to prevent ERR_INSUFFICIENT_RESOURCES when making AJAX requests

It's been a while since I dabbled in JavaScript, so please be patient with me. I'm currently developing an application that generates reports on student data using PHP as the backend. Periodically, we need to refresh the database used for these ...

There seems to be a problem with the text-to-speech API: It looks like there's a ReferenceError stating that

Currently, I am developing a program using the Quasar framework which is based on Vue and can be compiled for mobile using Cordova. However, I am encountering some issues when trying to run it on mobile. Below is the function causing problems: activat ...

Navigate through a given value in the event that the property undergoes a name change with each

Exploring an example found on the JSON site, I am facing a situation where I am using an API with JSON data. The property name "glossary" changes for each request made. For instance, if you search for "glossary", the first property is glossary. However, if ...

Using elegant query to group by month and calculate the count of each unique value

I'm currently working on a query to display the usage of medications by month over the past 6 months for each medication group. The output query should appear as follows: medication | October | September | August | July | June | May 1 ...

What is the best scenario to implement ftp_pasv?

Is it necessary to set ftp_pasv to TRUE? Can this setting be enabled all the time or are there certain scenarios where it shouldn't be used? Additionally, is there a method to automatically determine if the server requires this setting? ...

Errors encountered by the Chrome extension

Hey there, I've recently delved into creating a chrome extension but hit a roadblock. My issue revolves around the service worker registration failing and encountering errors related to undefined properties. https://i.stack.imgur.com/bGzB4.png The c ...

Serve static files using ExpressJS when a post request is made

My Express server is set up to serve static files for my website and it's working fine: var express = require('express'); var app = express(); var path = require('path'); var p = path.join(__dirname, '../web/public'); app ...

AngularJS is encountering an issue with the callback function, resulting in an error

Currently, I am utilizing the $timeout service in Angular to decrease a variable from 100 to 1 in increments of 1/10 seconds. Although I understand that using the $interval service would be a simpler solution, for this particular scenario, I am focused on ...

What is the best approach to storing and retrieving special characters ('+<>$") from a textfield into a database using PHP?

I have a form where users can enter a personal message with a subject. The data entered in the textarea is passed to a Javascript/jQuery function, which then sends it to a PHP file for storage in a database. However, I am encountering issues when special c ...

What is the reason behind the inconsistent behavior of Javascript's Date.getDate() and .setDate() methods?

As a hobbyist coder, I'm facing a challenging problem with building a dynamic HTML/CSS calendar. My goal is to have cells filled in based on today's date. However, when I attempt to add days to fill in another 13 days by looping through HTML elem ...

The sorting icon in jQuery Data Table's search option is not functioning

I am having an issue with jQuery DataTables. When using jQuery DataTables, it provides a default search option. However, the problem arises when I search for a particular record and if the content does not match or if I find a single record, then I need to ...

Angular Build Showing Error with Visual Studio Code 'experimentalDecorators' Configuration

Currently experiencing an issue in VSC where all my typescript classes are triggering intellisense and showing a warning that says: "[ts] Experimental support for is a feature that is subject to change in a future build. Set the 'experimentalDeco ...

Incorporate a new item into an array within DynamoDB that does not currently

I am attempting to update an attribute called items, which is a list of strings. Is it possible to update (append) the attribute only if it does not already exist? Something like a combination of list_append and if_not_exists. var params = { ... Upda ...

Calculate the total of all values associated with a dynamically generated key within an array of objects

I'm trying to calculate the sum of similar keys in an array of objects. Each object in the array will have some common keys, but not all arrays will share the same set of keys. I'm considering storing these keys in a separate array and then loopi ...

Utilizing AJAX to showcase an HTML popup

I am currently working on a project to create an HTML page that will display another HTML file in an alert when a button is pressed. However, I am facing an issue where the content is not being displayed as expected. <html> <head> ...

Scope binding is successful, but accessing the array is only possible after adding an Alert() function

Within my Angular controller, I'm utilizing the SharePoint JavaScript Object Model to fetch data from the Taxonomy (term store). Due to SharePoint's JSOM not being a conventional Angular function that can be easily understood by the scope, I util ...

Ways to extract a link from an image tag

$image = '<img src="http://some-img-link" alt="some-img-alt"/>'; $source = preg_match('/<img src=\"(.*?)\">/', $image); echo $source; Can someone help me retrieve the src value from the img tag as well as possib ...