[jQuery Post在来自Android应用程序的Cordova中失败

问题描述 投票:0回答:1

我正在使用Cordova 9.0.1:

[luis@luis myapp]$ cordova -v
9.0.0 ([email protected])

我无法解决此问题。每当我尝试使用简单的jQuery Post请求从Android应用程序登录到我们的服务器时,都会输入fail()方法。该请求未登录到Apache访问日志中,因此它永远不会到达服务器。

但是,如果我运行cordova并从浏览器尝试,则登录成功。

这是我的config.xml文件:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.experienceerp" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>MyAPP</name>
    <description>
        Description
    </description>
    <author email="[email protected]" href="http://mycompany.com">
        My company
    </author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
        <icon density="ldpi" src="res/icon/android/icon-36-ldpi.png" />
        <icon density="mdpi" src="res/icon/android/icon-48-mdpi.png" />
        <icon density="hdpi" src="res/icon/android/icon-72-hdpi.png" />
        <icon density="xhdpi" src="res/icon/android/icon-96-xhdpi.png" />
        <splash density="port-hdpi" src="res/screen/android/drawable-port-hdpi-screen.png" />
        <splash density="port-ldpi" src="res/screen/android/drawable-port-ldpi-screen.png" />
        <splash density="port-mdpi" src="res/screen/android/drawable-port-mdpi-screen.png" />
        <splash density="port-xhdpi" src="res/screen/android/drawable-port-xhdpi-screen.png" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <icon height="57" src="res/icon/ios/icon-57.png" width="57" />
        <icon height="114" src="res/icon/ios/icon-57-2x.png" width="114" />
        <icon height="72" src="res/icon/ios/icon-72.png" width="72" />
        <icon height="144" src="res/icon/ios/icon-72-2x.png" width="144" />
        <splash height="1024" src="res/screen/ios/screen-ipad-portrait.png" width="768" />
        <splash height="2048" src="res/screen/ios/screen-ipad-portrait-2x.png" width="1536" />
        <splash height="480" src="res/screen/ios/screen-iphone-portrait.png" width="320" />
        <splash height="960" src="res/screen/ios/screen-iphone-portrait-2x.png" width="640" />
        <splash height="1136" src="res/screen/ios/screen-iphone-portrait-568h-2x.png" width="640" />
    </platform>
    <preference name="Orientation" value="portrait" />
    <preference name="AutoHideSplashScreen" value="true" />
    <preference name="SplashScreenDelay" value="3000" />
    <preference name="SplashMaintainAspectRatio" value="true" />
    <preference name="SplashShowOnlyFirstTime" value="false" />
    <preference name="AndroidPersistentFileLocation" value="Compatibility" />
    <!--
    <plugin name="com.hutchind.cordova.plugins.streamingmedia" spec="~0.1.4" />
-->
    <plugin name="uk.co.workingedge.phonegap.plugin.launchnavigator" spec="^4.1.3" />
    <plugin name="cordova-plugin-camera" spec="^4.0.3" />
    <plugin name="cordova-plugin-tts" spec="^0.2.3" />
    <plugin name="cordova-plugin-badge" spec="^0.8.7" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
    <plugin name="cordova-plugin-vibration" spec="^3.1.0" />
    <plugin name="cordova-plugin-splashscreen" spec="^5.0.2" />
    <plugin name="cordova-plugin-network-information" spec="^2.0.1" />
    <plugin name="cordova-plugin-media-capture" spec="^3.0.2" />
    <plugin name="cordova-plugin-media" spec="^5.0.2" />
    <plugin name="cordova-plugin-local-notification" spec="^0.9.0-beta.2" />
    <plugin name="cordova-plugin-inappbrowser" spec="^3.0.0" />
    <plugin name="cordova-plugin-file" spec="^6.0.1" />
    <plugin name="cordova-plugin-dialogs" spec="^2.0.1" />
    <plugin name="cordova-plugin-device" spec="^2.0.2" />
    <plugin name="cordova-plugin-blob-constructor-polyfill" spec="^1.0.2" />
    <plugin name="cordova-plugin-app-event" spec="^1.2.1" />
    <plugin name="cordova-plugin-advanced-http" spec="^2.0.1" />
    <plugin name="cordova-plugin-actionsheet" spec="^2.3.3" />
    <plugin name="cordova-plugin-file-transfer" spec="^1.7.1" />
    <plugin name="cordova-plugin-geolocation" spec="^4.0.1" />
    <plugin name="cordova-plugin-mauron85-background-geolocation" spec="^3.0.0-alpha.47">
        <variable name="GOOGLE_PLAY_SERVICES_VERSION" value="11+" />
        <variable name="ANDROID_SUPPORT_LIBRARY_VERSION" value="26+" />
        <variable name="ICON" value="@mipmap/icon" />
        <variable name="SMALL_ICON" value="@mipmap/icon" />
        <variable name="ACCOUNT_NAME" value="@string/app_name" />
        <variable name="ACCOUNT_LABEL" value="@string/app_name" />
        <variable name="ACCOUNT_TYPE" value="$PACKAGE_NAME.account" />
        <variable name="CONTENT_AUTHORITY" value="$PACKAGE_NAME" />
    </plugin>
    <engine name="browser" spec="^5.0.4" />
    <engine name="ios" spec="^4.5.5" />
    <engine name="cordova-android" version=">=1.8.0" />
</widget>

您可以检查,它具有用于跨域访问的标签:

<access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />

我甚至将此标签添加到应用程序index.html的入口点:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src *">

我将其视为其他地方的解决方案,但仍然无法正常工作。

这是一段失败的代码。应用程序控制台中未显示任何错误消息(我正在将设备插入计算机后对其进行测试,以便可以检查控制台):

console.log('Post to ' + url + ' user:' + data.user + ' password:' + data.password); // both the URL and the data are correct
$.post(url, data, function(res) {
    // This code is not reached
    if (res.id !== undefined && res.token) {
        console.log('Login OK');
        // store user data...
        return;
    }
    if (res.error) {
        // alert the validation error
        return;
    }
    alert('Unexpected server error');
}).fail(function(jqXHR, textStatus, errorThrown) {
    console.log('Login error: ' + textStatus + ' ' + errorThrown);
    // show error in login form...
});

控制台中显示的错误是:登录错误:错误 未找到

您能帮我吗?我被困住了,什么都没想到,我已经做了发现的所有事情。

android jquery cordova phonegap
1个回答
0
投票

我找到了解决方法:

从Cordova 9,API级别28开始,所有具有Android 9或更高版本的设备都被迫发出https请求而不是http,除非在异常配置中指定,否则它将始终失败,如此处所述:

Security config

更多信息here

© www.soinside.com 2019 - 2024. All rights reserved.