【问题标题】:Cordova Whitelist PluginCordova 白名单插件
【发布时间】:2017-04-16 19:15:16
【问题描述】:

我知道这个问题已经在网上被问过很多次了。我尝试了所有的方法都没有运气了。

我正在尝试将数据发布到服务器并返回一个简单的响应。 显然我需要安装cordova白名单插件来实现从应用程序访问外部资源,所以我安装了它。

我得到的错误是: 加载资源失败:net::ERR_NAME_NOT_RESOLVED

项目信息:

  • 这是一个温泉 UI 应用程序
  • 科尔多瓦 CLI 6.5.0
  • Android 版本 6.1.2

已安装的插件:

  • cordova-plugin-compat@1.1.0
  • cordova-plugin-file@~4.3.2
  • cordova-plugin-splashscreen@4.0.2
  • cordova-plugin-camera@2.4.0
  • cordova-plugin-whitelist@1.3.3-dev

元标记:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

我正在使用的工具: 视窗 10, 视觉工作室 2015

我做了什么:

  1. 使用 Visual 卸载并重新安装白名单插件 工作室

  2. 卸载插件并通过 Cordova CLI 重新安装

    cordova插件添加cordova-plugin-whitelist

  3. 通过 Github 卸载并重新安装插件

  4. 我什至将 Cordova 从 6.2.0 更新到 6.5.0

  5. 我创建了一个简单的 GET 请求以查看它是否有效,但仍然没有任何效果。

  6. 已清除 Visual Studio 中的 Cordova 缓存。

我使用的Javascript代码:

$http({
    method: 'GET', url: "http://mywebsite.com/simple_get.php"
}).then(
   function (response) {
       alert(JSON.stringify(response));
   },
   function (response) {
       alert(JSON.stringify(response));
   }
);

jQuery 代码也试过了:

var settings = {
    "async": true,
    "crossDomain": true,
    "url": "http://mywebsite.com/demo.php",
    "method": "GET",
    "headers": {
        "content-type": "application/x-www-form-urlencoded",
        "cache-control": "no-cache"
    },
    "data": {}
}

jQuery.ajax(settings).done(function (response) {
    console.log(response);
});

我不知道如何调试并解决问题,任何帮助将不胜感激......这是我的应用程序完成的最后一步:(

谢谢

【问题讨论】:

    标签: angularjs cordova cordova-plugins onsen-ui whitelist


    【解决方案1】:

    这是我的 config.xml

    <?xml version="1.0" encoding="utf-8"?>
    <widget id="au.com.myapp" version="1.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps">
      <name> MyApp</name>
      <description>fancy user interfaces for hybrid mobile applications. It uses uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.</description>
      <author email="admin@myappdomain.com" href="http://myappdomain.com">Author</author>
      <content src="index.html" />
      <access origin="*" />
      <allow-intent href="*" />
      <allow-intent href="http://*/*" />
      <allow-intent href="https://*/*" />
      <allow-intent href="tel:*" />
      <allow-intent href="sms:*" />
      <allow-intent href="mailto:*" />
      <allow-intent href="geo:*" />
      <allow-navigation href="*" />
      <allow-intent href="itms:*" />
      <allow-intent href="itms-apps:*" />
      <vs:features />
      <preference name="windows-target-version" value="8.1" />
      <preference name="windows-phone-target-version" value="8.1" />
      <preference name="DisallowOverscroll" value="true" />
      <preference name="Orientation" value="portrait" />
      <preference name="loglevel" value="DEBUG" />
      <preference name="AndroidLaunchMode" value="singleTop" />
      <preference name="ErrorUrl" value="" />
      <preference name="Fullscreen" value="True" />
      <preference name="KeepRunning" value="true" />
      <preference name="SplashScreen" value="screen" />
      <preference name="AutoHideSplashScreen" value="false" />
      <preference name="SplashScreenDelay" value="20000" />
      <preference name="FadeSplashScreen" value="false" />
      <preference name="FadeSplashScreenDuration" value=".25" />
      <preference name="ShowSplashScreenSpinner" value="false" />
      <preference name="AllowInlineMediaPlayback" value="false" />
      <preference name="BackupWebStorage" value="cloud" />
      <preference name="EnableViewportScale" value="false" />
      <preference name="KeyboardDisplayRequiresUserAction" value="true" />
      <preference name="MediaPlaybackRequiresUserAction" value="false" />
      <preference name="SuppressesIncrementalRendering" value="false" />
      <preference name="TopActivityIndicator" value="gray" />
      <preference name="GapBetweenPages" value="0" />
      <preference name="PageLength" value="0" />
      <preference name="PaginationBreakingMode" value="page" />
      <preference name="PaginationMode" value="unpaginated" />
      <feature name="LocalStorage">
        <param name="ios-package" value="CDVLocalStorage" />
      </feature>
      <preference name="UIWebViewDecelerationSpeed" value="normal" />
      <preference name="monaca:AndroidIsPackageNameSeparate" value="false" />
      <preference name="monaca:targetFamilyiPhone" value="1" />
      <preference name="monaca:targetFamilyiPad" value="1" />
      <platform name="android">
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi/icon.png" />
        <icon density="mdpi" src="resources/android/icon/drawable-mdpi/icon.png" />
        <icon density="hdpi" src="resources/android/icon/drawable-hdpi/icon.png" />
        <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi/icon.png" />
      </platform>
      <platform name="android">
        <splash density="land-hdpi" src="resources/android/drawable-land-hdpi/screen.png" />
        <splash density="land-ldpi" src="resources/android/drawable-land-ldpi/screen.png" />
        <splash density="land-mdpi" src="resources/android/drawable-land-mdpi/screen.png" />
        <splash density="land-xhdpi" src="resources/android/drawable-land-xhdpi/screen.png" />
        <splash density="port-hdpi" src="resources/android/drawable-port-hdpi/screen.png" />
        <splash density="port-ldpi" src="resources/android/drawable-port-ldpi/screen.png" />
        <splash density="port-mdpi" src="resources/android/drawable-port-mdpi/screen.png" />
        <splash density="port-xhdpi" src="resources/android/drawable-port-xhdpi/screen.png" />
      </platform>
      <platform name="ios">
        <splash height="480" src="res/screen/ios/Default~iphone.png" width="320" />
        <splash height="960" src="res/screen/ios/Default@2x~iphone.png" width="640" />
        <splash height="1024" src="res/screen/ios/Default-Portrait~ipad.png" width="768" />
        <splash height="2048" src="res/screen/ios/Default-Portrait@2x~ipad.png" width="1536" />
        <splash height="768" src="res/screen/ios/Default-Landscape~ipad.png" width="1024" />
        <splash height="1536" src="res/screen/ios/Default-Landscape@2x~ipad.png" width="2048" />
        <splash height="1136" src="res/screen/ios/Default-568h@2x~iphone.png" width="640" />
        <splash height="1334" src="res/screen/ios/Default-667h.png" width="750" />
        <splash height="2208" src="res/screen/ios/Default-736h.png" width="1242" />
        <splash height="1242" src="res/screen/ios/Default-Landscape-736h.png" width="2208" />
        <splash src="res/screen/ios/Default@2x~universal~anyany.png" />
        <splash src="res/screen/ios/Default@2x~universal~comany.png" />
        <splash src="res/screen/ios/Default@2x~universal~comcom.png" />
        <splash src="res/screen/ios/Default@3x~universal~anyany.png" />
        <splash src="res/screen/ios/Default@3x~universal~anycom.png" />
        <splash src="res/screen/ios/Default@3x~universal~comany.png" />
      </platform>
      <plugin name="cordova-plugin-camera" spec="~2.4.0" />
      <plugin name="cordova-plugin-compat" version="1.1.0" />
      <plugin name="cordova-plugin-file" spec="~4.3.2" />
      <plugin name="cordova-plugin-inappbrowser" version="1.7.0" />
      <plugin name="cordova-plugin-splashscreen" version="4.0.2" />
      <plugin name="cordova-plugin-whitelist" version="1.3.2" />
    </widget>
    

    【讨论】:

    • 这看起来不错。 应该允许您访问应用程序之外的任何 URL。
    • 我什么都试过了。我从头开始创建了一个空白应用程序,然后用一个简单的 GET 添加了白名单插件,但这也不起作用。可以是PC配置吗?
    • 你是如何测试的?在设备或模拟器上?您可以在 chrome 中测试并检查控制台吗?
    • 在 Chrome 上可以正常工作,通过 Ripple 也可以正常工作。只有在实际设备上安装 apk 时才会出错
    • 我已经更新了我的答案,其中包含有关捕获和忽略状态 0 错误的信息。
    【解决方案2】:

    编辑 2:

    在 Ajax 函数上方的任何位置尝试此代码。

    $(document).ajaxError(function(e, jqxhr, settings, exception) { 
        if (jqxhr.readyState == 0 || jqxhr.status == 0) {
             return; 
        }
     });
    

    这将捕获状态 0 错误并允许 jQuery 忽略它并继续 Ajax 请求。

    仅用于调试目的。

    编辑

    试试下面的。

    $.ajax({
       url: 'YourRestEndPoint',
       headers: {'yourHeaderKey': 'yourHeaderValue',
    'yourHeaderKey2': 'yourHeaderValue2'},
       method: 'POST',
       data: {},
       success: function(data){
            console.log('succes: '+data);
        },
        error: function (jqXHR, textStatus, errorThrown){
    
            console.log(jqXHR + " " + textStatus + " " + errorThrown);
    
            console.log(jqXHR.status);
    
        }
    });
    

    这应该会更好地记录错误。

    我从您的问题中看到您正在尝试将数据发布到服务器,但是在您使用 GET 请求的代码中。端点接受 POST 和 GET 还是只接受 POST。

    原创 如果您遇到“名称未解析”错误,您是否确保 URL 正确?您是否在您可以控制的服务器上运行端点?它是由您还是第三方托管?您的 DNS 查找有问题吗?如果您尝试使用已知的工作端点,相同的代码是否可以正常工作?

    可能是由很多或多种因素造成的。

    白名单插件应自动安装在最新版本的 Cordova 上。

    【讨论】:

    • 网址是正确的,我用邮递员打过网址,有反应。我可以完全控制端点服务器,DNS 查找很好。端点在 Ripple Emulator 内本地运行良好。我该如何调试呢?
    • 查看我的编辑以获得更好的错误记录。还可以使用 jQuery 尝试我的 Ajax 请求格式。
    • 它接受POST和GET,我试过你发给我的JS。 jqHXR 的输出是:[object Object] controllers.js (111,25) { [functions]: , __proto__: { }, readyState: 0, status: 0, statusText: error }
    • 我添加了一行来记录状态。你能试试,告诉我你得到了什么。
    • 它在我上面的评论中返回一个零 (0) 你会看到状态被记录为 status: 0
    猜你喜欢
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多