Cordova AJAX调用仅在发布版本上失败

Mar*_*ani 5 ajax android cordova

我正在开发一个用于android的Cordova应用程序,我有一个关于jquery ajax调用的奇怪问题:调试版本没有问题,而在发行版中,每个ajax调用都失败并出现错误.如果我使用USB在设备上运行一切都很好.如果我在调试模式下编译应用程序,将其复制并安装在设备上,一切都很好.

如果我在发布模式下编译应用程序并使用jarsigner和zipalign签名发布,并将其复制并安装在设备上,则相同的ajax调用将失败,其中readyState 0,responseText为空,状态为0,statusText为"error".

我花了一周时间搜索并尝试在stackoverflow和google上发现的所有类似问题,但没有任何改变.此主题主题类似,但我的SSL证书似乎没问题,并且没有提供其他解决方案: 在发布模式下运行Android Cordova应用程序时,Ajax调用失败

Cordova版本是6.5(但我也有6.4的问题).

这是app中的ajax调用:

glang = window.localStorage.getItem('glang');

show_loader();

$.support.cors=true;
$.ajax({
  url: "https://app.laureatedesign.com/wp-content/plugins/designnews/ajax_langs.php",
  data: "act=aj_app_lng_lst&lang="+glang+"&xend=x",
  type: "POST",
  dataType: "json",
  timeout: 10000,
  success: function(results) {
      if (results["TYPE"]=="OK") {
          $('#main').html(results["CONTENT"]);
          $('#main').imagesLoaded( { },
            function() {
              show_content();
            }
          );
      }
      if (results["TYPE"]=="ERR") {
          show_error();
      }
      },
  error: function(XMLHttpRequest, status) {
     console.log(status);
     switch(status) {
      default:
          show_error();
       break;
      case "timeout":
          show_error();
       break;
     }
  }
});
Run Code Online (Sandbox Code Playgroud)

无论如何,服务器上的ajax文件ajax_langs.php可以简化如下:

<?php
  $res = array("TYPE"=>"OK", "CONTENT"=>"Test");

  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Methods: GET, POST');
  echo json_encode($res);
?>
Run Code Online (Sandbox Code Playgroud)

这是我的config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.naba.designnews" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>APPTITLE</name>
    <description>
        DESCRIPTION
    </description>
    <author email="EMAIL" href="WEBSITE">
        AUTHOR
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />
    <plugin name="InAppBrowser" value="CDVInAppBrowser" />
    <allow-navigation href="*" />
    <allow-navigation href="*://*youtube.com" />
    <access origin="*" />
    <access origin=".*" />
    <access origin="*.pushwoosh.com" />
    <access origin="http://*" />
    <access origin="https://*" />
    <access origin="https://laureatedesign.com" subdomains="true" />
    <allow-intent href="http://*" />
    <allow-intent href="https://*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <preference name="windows-target-version" value="10.0" />
    <preference name="StatusBarOverlaysWebView" value="false" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>
Run Code Online (Sandbox Code Playgroud)

我添加了白名单插件.在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'; script-src * 'self' 'unsafe-inline'; connect-src * https: http:">
Run Code Online (Sandbox Code Playgroud)

我也尝试删除android平台并再次添加它.没有变化.

正如我在调试模式中所说的一切都很好,所有ajax调用都成功返回.要在Google Play上发布应用,我正在进行签名并创建包,如下所示:

1)cordova build android --release

2)jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore"PATH-TO-APP/APP.keystore""PATH-TO-APP/platforms/android/build/outputs/apk/android-release-unsigned.apk"designnews

3)zipalign -v 4"PATH-TO-APP/platforms/android/build/outputs/apk/android-release-unsigned.apk""PATH-TO-APP/platforms/android/build/outputs/apk/AppTitle. APK"

我知道这是一个涉及CORS和所有这些东西的跨域调用.但是为什么调试版本没问题,而发行版本却没有?

请提前帮助,谢谢.

小智 2

我知道它\xc2\xb4s是一个3年前的问题,但经过深入搜索后找到了解决方案,并且我想分享它。

\n\n

我遇到了您报告的同样问题,解决方案非常简单;

\n\n

1\xc2\xba - 您需要运行“cordovaprepareandroid”来获取java二进制文件。

\n\n

2\xc2\xba - 在 Android Studio 上打开输出该命令的代码,导航到文件夹 manifests/AndroidManifest.xml

\n\n

3\xc2\xba - 有一个叫做“应用程序”,你只需要添加 android:usesCleartextTraffic="true"

\n\n

4\xc2\xba - 构建并签署您的应用程序,现在它应该可以毫无问题地运行 ajax!

\n