簡體   English   中英

使用 Ajax 調用將 Cordova 9 移動應用程序連接到 Java Web 服務

[英]Connect Cordova 9 mobile app to Java Web Service using Ajax call

我對 Cordova 很陌生,我已經安裝了我在 android studio 上開發的 Cordova 版本 9。 我對用 Java 編寫的 Web 服務層進行了簡單的 ajax 調用。 由於某種原因,我無法從我的移動應用程序連接到我的 Web 服務,它返回 404 狀態。 相同的 url 是從模擬器的瀏覽器連接的,但不是從開發的應用程序連接的。 我已在 config.xml 中將我的 url 列入白名單。 下面是 config.xml 和 ajax 代碼。

配置文件

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloWorld</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <preferance name="android-usesCleartextTraffic" value="true" />
    <access origin="*" subdomains="true"/>
    <access origin="http://192.168.0.111:8091" subdomains="true"/>
    <allow-navigation href="http://*/*" />
    <allow-navigation href="https://*/*" />
    <allow-navigation href="data:*" />
    <allow-navigation href="http://192.168.0.111:8091/*" />
    <allow-navigation href="*" />

    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="http://192.168.0.111:8091/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">        
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
        </edit-config>
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

阿賈克斯代碼:

document.getElementById("btnLogin").addEventListener("click", onClickOfLogin);
    function onClickOfLogin(){
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            url: "http://192.168.0.111:8091/ZeOMSWS/AppLogin",
            crossDomain: true,
            success: function(resultObj) {
                navigator.notification.alert(
                    'Registration request has been sent for HR approval. You will receive an email once approved and then you can proceed with Login.', 
                    onSuccessDismissOfAlert, 
                    'Info', 
                    'OK');
            },
            error: function(request, status, error) {
                console.log("Error status " + status);
                console.log("Error request status text: " + request.statusText);
                console.log("Error request status: " + request.status);
                console.log("Error request response text: " + request.responseText);
                console.log("Error response header: " + request.getAllResponseHeaders());
                navigator.notification.alert(
                    'Error Occured.' + error, 
                    onErrorDismissOfAlert, 
                    'Info', 
                    'OK');
            }
        });
    }

正如我之前提到的,我使用的是帶有 SDK 版本 29 的 Cordova 9,模擬器在 Android 10 上。我已經堅持了 2 天多。 我在谷歌上嘗試了很多解決方案,但我還是不明白我哪里出錯了。 請幫助我知道我缺少什么。

任何幫助將不勝感激。

謝謝

問題在於,在 Android SDK 版本 29 中,默認情況下允許對 https 服務進行 ajax 調用。

無法發送 HTTP 請求。

只需將應用程序和服務器響應中的 URL 從 http:// 更改為 https://

除此以外:

  1. config.xml 中添加小部件:
    <widget xmlns:android="http://schemas.android.com/apk/res/android">
  1. config.xml 中編輯平台 android :
    <platform name="android">
        <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
        </edit-config>
    </platform>

希望有用

在 Android P 1 中默認使用 TLS 保護用戶

Android高版本http網絡請求失敗的Cordova配置處理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM