iT邦幫忙

0

關於 無法在 cordova android 中使用 jquery ajax

  • 分享至 

  • xImage

您好,我使用的是 cordova 11.0.0 版。
我發現在 www 目錄中的網頁如果有使用到 jquery 的 ajax post 呼叫都會失效。
例如以下程式碼 :

$.ajax({
        method: 'POST',
        url: 'http://192.168.1.3/plant_ajax/',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        dataType: "json",
        data: {RegID:1},
        retryCount: 3,
        success : function(response) {
               
		  console.log(response);

        },
	error : function(err){

		console.log(err);

	}
     });

我在 Android 的 ADV 模擬器中用 chrome 瀏覽器 直接在網址列輸入 http://192.168.1.3/plant_ajax/ 這一段網址是有資料回應的。
但是在 cordova build android 建置好 android apk 之後,執行 cordova run android 在 ADV 中執行,卻都會發生
以上那一段程式碼無法連線到 http://192.168.1.3/plant_ajax/

似乎在 cordova 的 android 應用程式中無法使用 jquery 的 ajax 呼叫。

請問是否有解決方式?
感謝您的回覆。

看更多先前的討論...收起先前的討論...
猜測...會不會是要用 https
天黑 iT邦研究生 5 級 ‧ 2022-02-16 09:40:31 檢舉
這邊有討論 可以試試看 cors 、權限設定等等.. https://stackoverflow.com/questions/33024145/cordova-jquery-ajax-not-working
Homura iT邦高手 1 級 ‧ 2022-02-16 17:52:01 檢舉
你的api action是post和get都行嗎?
直接打網址是用get...
我這兩天有查到最多的建議方式是說 安裝 white-list 這一個插件,但是我在執行
cordova plugin add cordova-plugin-whitelist 這一個安裝指令的時候卻會出現以下類似警告訊息 :
---------------------------------------------------------------------------------------------
Unmet project requirements for latest version of cordova-plugin-whitelist:
cordova-android (10.1.1 in project, >=4.0.0 <10.0.0 required)
Fetching highest version of cordova-plugin-whitelist that this project supports: 1.3.4 (latest is 1.3.5)
Installing "cordova-plugin-whitelist" for android
Adding cordova-plugin-whitelist to package.json
--------------------------------------------------------------
然後 執行 : cordova build android 就會出現以下錯誤訊息 :
--------------------------------------------------------------------------
Task :app:compileDebugJavaWithJavac FAILED
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:25: error: cannot find symbol
import org.apache.cordova.Whitelist;
^
symbol: class Whitelist
location: package org.apache.cordova
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:32: error: cannot find symbol
private Whitelist allowedNavigations;
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:33: error: cannot find symbol
private Whitelist allowedIntents;
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:34: error: cannot find symbol
private Whitelist allowedRequests;
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:48: error: cannot find symbol
public WhitelistPlugin(Whitelist allowedNavigations, Whitelist allowedIntents, Whitelist allowedRequests) {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:48: error: cannot find symbol
public WhitelistPlugin(Whitelist allowedNavigations, Whitelist allowedIntents, Whitelist allowedRequests) {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:48: error: cannot find symbol
public WhitelistPlugin(Whitelist allowedNavigations, Whitelist allowedIntents, Whitelist allowedRequests) {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:138: error: cannot find symbol
public Whitelist getAllowedNavigations() {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:142: error: cannot find symbol
public void setAllowedNavigations(Whitelist allowedNavigations) {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:146: error: cannot find symbol
public Whitelist getAllowedIntents() {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:150: error: cannot find symbol
public void setAllowedIntents(Whitelist allowedIntents) {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:154: error: cannot find symbol
public Whitelist getAllowedRequests() {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:158: error: cannot find symbol
public void setAllowedRequests(Whitelist allowedRequests) {
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:41: error: cannot find symbol
this(new Whitelist(), new Whitelist(), null);
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:41: error: cannot find symbol
this(new Whitelist(), new Whitelist(), null);
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:45: error: cannot find symbol
this(new Whitelist(), new Whitelist(), null);
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:45: error: cannot find symbol
this(new Whitelist(), new Whitelist(), null);
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:50: error: cannot find symbol
allowedRequests = new Whitelist();
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:61: error: cannot find symbol
allowedNavigations = new Whitelist();
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:62: error: cannot find symbol
allowedIntents = new Whitelist();
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
D:\MobileDev\Cordova\SHH\CordovaTest\Android\platforms\android\app\src\main\java\org\apache\cordova\whitelist\WhitelistPlugin.java:63: error: cannot find symbol
allowedRequests = new Whitelist();
^
symbol: class Whitelist
location: class org.apache.cordova.whitelist.WhitelistPlugin
21 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
我直接打開 cordova 專案下的 www 目錄的網頁,是可以正常透過 ajax 呼叫到 Server 端的。
目前可以確認的是,只要做 :
(1) cordova build android
(2) cordova run android
(3) 在 Android Studio 的 ADV 手機模擬器裡執行 ,可以取得 ajax 的 error 事件的 返回資料訊息如下 :
{
"readyState" : 0,
"status" : 0,
"statusText":"error"
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答