顯示具有 cordova 標籤的文章。 顯示所有文章
顯示具有 cordova 標籤的文章。 顯示所有文章

2019年1月14日 星期一

cordova向php發出ajax請求時出現No 'Access-Control-Allow-Origin' header is present on the requested resource錯誤

雖然請求還是成功發出,但是會無法收到請求後的msg回覆
也可能導致在裝置上執行出問題
解決方式是在伺服器端的php程式上加上下列這行
header("access-control-allow-origin: *");

2019年1月8日 星期二

cordova 寫在index內的javascript無法執行

以cordova開發app時,在檔案內直接寫入如下
<script>
....
</script>

在Chrome的dev tool中檢查時出現下面的錯誤:
"cordova script Refused to execute inline script because it violates the following Content Security Policy....."

這是因為要降低XSS漏洞的風險,所以禁止了inline的javascript,這會使chrome把直接寫在index內的javascript語法以及一些行內事件如onClick的事件執行都擋住。

解決方法有兩種:

1.啟用行內的JS
在cordova專案的index.html上面也有標注:
Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
所以只要把unsafe-inline 加進 default-src內即可,如下
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
2.把script另外寫成一個js檔案,再連結進來即可

2018年7月7日 星期六

Android studio使用emulate Android模擬器時出現device still connecting bug臭蟲的修正方式

狀況:
終端機使用$ cordova emulate android指令時,無法正常開啟並執行Android studio的模擬器

錯誤碼:

(node:1225) UnhandledPromiseRejectionWarning: CordovaError: Failed to execute shell command "getprop,dev.bootcomplete"" on device: Error: adb: Command failed with exit code 1 Error output:
error: device still connecting

修正方式:
Step1.
開啟電腦資料夾至APP專案內下面這個位置的emulator.js檔案
yourAppName/platforms/android/cordova/lib/emulator.js

Step2.
以編碼軟體開啟emulator.js檔案,並且找到下面語法
if ((error && error.message &&
    (error.message.indexOf('not found') > -1)) ||
    (error.message.indexOf('device offline') > -1))

Step3.
把上面這段語法改為如下:(紅色字標註是新增的部分)
if ((error && error.message &&
    (error.message.indexOf('not found') > -1)) ||
    (error.message.indexOf('device offline') > -1) ||
    (error.message.indexOf('device still connecting') > -1))

Step4.
修改完並且儲存後,再次於終端機執行模擬指令
$ cordova emulate android
即可順利開啟模擬器

Reference:
  1. https://stackoverflow.com/questions/50935337/failed-to-execute-shell-command-getprop-dev-bootcomplete-on-device-error-for
  2. https://issues.apache.org/jira/browse/CB-14165
  3. https://github.com/apache/cordova-android/pull/457/files