我在论坛中没有发现这个问题,所以我写了这个。
当我从服务器上获取远程内容时,我的FirefoxOS设备(Geekspophone开发者预览版)中的FirefoxOS应用程序出现问题。
从我的JS文件开始,我就用AJAX调用服务器中的PHP文件,它会在我的HTML文件中打印contentResult。这是正确的。
这是javascript文件内容
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");
}
这是HTML文件内容
<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>
这是PHP文件内容
<?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>";
?>
所以,问题是:
- 当我调用PHP文件时,通常创建的按钮没有可点击的功能,在web和FxOS应用程序中都没有
- "a"元素也是在对PHP文件的调用中创建的,在它有可点击的功能[显示警报和日志],但在应用程序中安装在FxOS设备中,它什么都不做
在这个链接中,你可以测试我试图向你解释的内容。如果你想查看代码,有一个zip文件。zip文件
谢谢你抽出时间。如果你不明白什么,问我。
听起来像是违反了CSP(https://developer.mozilla.org/en-US/Apps/Build/Building_apps_for_Firefox_OS/CSP)你不能用onclick。
这是根据CORS规范第7.3节:设计的
- 由于证书错误,不允许使用https
使用自签名证书被视为证书错误。尝试使用systemXHR客户端,而不是CORS服务器端。