【问题标题】:PhoneGap AJAX Calls NOT Working - December 2015PhoneGap AJAX 呼叫不工作 - 2015 年 12 月
【发布时间】:2015-12-30 21:29:46
【问题描述】:

在这里发帖:http://community.phonegap.com/nitobi/topics/jquery-ajax-calls-all-of-the-sudden-do-not-work

这是过去一两周的全新错误。基本上,我在 PhoneGap 中的 index.html 文件的头部有一个 ajax 调用,它的状态码为 0不工作。我还确认所有 AJAX 调用都不起作用。

我尝试了很多解决方案,但没有任何效果。

我试过了:

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

  • 添加了 $.support.cors = true;

  • 在服务器上的 htaccess 文件中添加了 Header set Access-Control-Allow-Origin "*"

  • 尝试了不同版本的白名单插件。

  • 尝试了不同形式的访问源设置。

  • 尝试将内联 JS 从 index.html 移动到单独的 JS 文件中。

  • 已验证该代码行在 localhost 和通过 Chrome 从不同的 Web 服务器中均有效。

请帮忙。

这是我的 config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
    <widget xmlns   = "http://www.w3.org/ns/widgets"
        xmlns:gap   = "http://phonegap.com/ns/1.0"
        id          = "xxxxxx"
        version     = "1.0.0" >

    <name>Test</name>
    <description>xxxx</description>
    <author href="xxxx" email="xxxx">xxxx</author>

    <!-- basic config -->

    <preference name='phonegap-version' value='cli-5.2.0' />
    <preference name="android-build-tool" value="ant" />

    <preference name="orientation"                value="portrait" />
    <!-- all: default means both landscape and portrait are enabled -->
    <preference name="fullscreen"                 value="false" />
    <!-- all: hides the status bar at the top of the screen -->
    <preference name="target-device"              value="universal" />
    <!-- ios: possible values handset, tablet, or universal -->
    <preference name="prerendered-icon"           value="true" />
    <!-- ios: if icon is prerendered, iOS will not apply it's gloss to the app's icon on the user's home screen -->
    <preference name="ios-statusbarstyle"         value="black-opaque" />
    <!-- ios: black-translucent will appear black because the PhoneGap webview doesn't go beneath the status bar -->
    <preference name="detect-data-types"          value="true" />
    <!-- ios: controls whether data types (such as phone no. and dates) are automatically turned into links by the system -->
    <preference name="exit-on-suspend"            value="false" />
    <!-- ios: if set to false, the splash screen must be hidden using a JavaScript API -->
    <preference name="disable-cursor"             value="false" />
    <!-- blackberry: prevents a mouse-icon/cursor from being displayed on the app -->


    <!-- START Splash Screen Config -->


    <!--<preference name="auto-hide-splash-screen" value="false" />
    <preference name="SplashScreenDelay" value="500000" />-->

    <gap:splash src="splash.png" />

    <!-- iPhone and iPod touch -->
    <gap:splash src="img/splash/Default.png" gap:platform="ios" width="320" height="480" />
    <gap:splash src="img/splash/Default@2x.png" gap:platform="ios" width="640" height="960" />

    <!-- iPhone 5 / iPod Touch (5th Generation) -->
    <gap:splash src="img/splash/Default-568h@2x.png" gap:platform="ios" width="640" height="1136" />

    <!-- iPhone 6 -->
    <gap:splash src="img/splash/Default-667h@2x.png" gap:platform="ios" width="750" height="1334" />
    <gap:splash src="img/splash/Default-Portrait-736h@3x.png" gap:platform="ios" width="1242" height="2208" />
    <gap:splash src="img/splash/Default-Landscape-736h@3x.png" gap:platform="ios" width="2208" height="1242" />

    <!-- iPad -->
    <gap:splash src="img/splash/Default-Portrait.png" gap:platform="ios" width="768" height="1024" />
    <gap:splash src="img/splash/Default-Landscape.png" gap:platform="ios" width="1024" height="768" />

    <!-- Retina iPad -->
    <gap:splash src="img/splash/Default-Portrait@2x.png" gap:platform="ios" width="1536" height="2048" />
    <gap:splash src="img/splash/Default-Landscape@2x.png" gap:platform="ios" width="2048" height="1536" />

    <!-- Android -->
    <gap:splash src="img/splash/port-ldpi.png" gap:platform="android" gap:qualifier="port-ldpi" />
    <gap:splash src="img/splash/land-ldpi.png" gap:platform="android" gap:qualifier="land-ldpi" />
    <gap:splash src="img/splash/port-mdpi.png" gap:platform="android" gap:qualifier="port-mdpi" />
    <gap:splash src="img/splash/land-mdpi.png" gap:platform="android" gap:qualifier="land-mdpi" />
    <gap:splash src="img/splash/port-hdpi.png" gap:platform="android" gap:qualifier="port-hdpi" />
    <gap:splash src="img/splash/land-hdpi.png" gap:platform="android" gap:qualifier="land-hdpi" />
    <gap:splash src="img/splash/port-xhdpi.png" gap:platform="android" gap:qualifier="port-xhdpi" />
    <gap:splash src="img/splash/land-xhdpi.png" gap:platform="android" gap:qualifier="land-xhdpi" />


    <!-- END Splash Screen Config -->

    <!-- allow background music play iOS -->
    <gap:config-file platform="ios" parent="UIBackgroundModes" overwrite="true">
      <array>    
             <string>audio</string>
      </array>
    </gap:config-file>

    <!-- enable permissions -->
    <preference name="permissions" value="none"/>
    <feature name="http://api.phonegap.com/1.0/file"/>
    <feature name="http://api.phonegap.com/1.0/geolocation"/>
    <feature name="http://api.phonegap.com/1.0/media"/>
    <feature name="http://api.phonegap.com/1.0/network"/>
    <feature name="http://api.phonegap.com/1.0/notification"/>

    <!-- Plugins -->
    <!--<plugin name="InAppBrowser" value="CDVInAppBrowser" />
    <feature name="InAppBrowser">
        <param name="ios-package" value="CDVInAppBrowser" />
    </feature>-->

    <!-- Core plugins -->
    <!--<gap:plugin name="org.apache.cordova.battery-status" />
    <gap:plugin name="org.apache.cordova.camera" />
    <gap:plugin name="org.apache.cordova.media-capture" />
    <gap:plugin name="org.apache.cordova.console" />
    <gap:plugin name="org.apache.cordova.contacts" />
    <gap:plugin name="org.apache.cordova.device-motion" />
    <gap:plugin name="org.apache.cordova.device-orientation" />
    <gap:plugin name="org.apache.cordova.dialogs" />
    <gap:plugin name="org.apache.cordova.file-transfer" />
    <gap:plugin name="org.apache.cordova.geolocation" />
    <gap:plugin name="org.apache.cordova.globalization" />
    <gap:plugin name="org.apache.cordova.inappbrowser" />
    <gap:plugin name="org.apache.cordova.network-information" />
    <gap:plugin name="org.apache.cordova.vibration" />-->

    <gap:plugin name="cordova-plugin-device" source="npm"/>
    <gap:plugin name="cordova-plugin-file" source="npm"/>
    <gap:plugin name="cordova-plugin-media" source="npm"/>
    <gap:plugin name="cordova-plugin-splashscreen" source="npm"/>
    <gap:plugin name="cordova-plugin-whitelist" source="npm"/>
    <gap:plugin name="cordova-plugin-x-socialsharing" source="npm"/>
    <gap:plugin name="cordova-plugin-admobpro" source="npm" spec="2.9.6"/>
    <!--<gap:plugin name="de.appplant.cordova.plugin.local-notification"/>-->

    <!-- Third party plugins -->
    <!-- A list of available plugins are available at https://build.phonegap.com/plugins -->
    <!--
    <gap:plugin name="com.phonegap.plugins.barcodescanner" />
    -->

    <!-- Define app icon for each platform. -->
    <!--<icon src="icon.png" />
    <icon src="res/icon/android/icon-36-ldpi.png"   gap:platform="android"    gap:density="ldpi" />
    <icon src="res/icon/android/icon-48-mdpi.png"   gap:platform="android"    gap:density="mdpi" />
    <icon src="res/icon/android/icon-72-hdpi.png"   gap:platform="android"    gap:density="hdpi" />
    <icon src="res/icon/android/icon-96-xhdpi.png"  gap:platform="android"    gap:density="xhdpi" />
    <icon src="res/icon/blackberry/icon-80.png"     gap:platform="blackberry" />
    <icon src="res/icon/blackberry/icon-80.png"     gap:platform="blackberry" gap:state="hover"/>
    <icon src="res/icon/ios/icon-57.png"            gap:platform="ios"        width="57" height="57" />
    <icon src="res/icon/ios/icon-72.png"            gap:platform="ios"        width="72" height="72" />
    <icon src="res/icon/ios/icon-57-2x.png"         gap:platform="ios"        width="114" height="114" />
    <icon src="res/icon/ios/icon-72-2x.png"         gap:platform="ios"        width="144" height="144" />
    <icon src="res/icon/webos/icon-64.png"          gap:platform="webos" />
    <icon src="res/icon/windows-phone/icon-48.png"  gap:platform="winphone" />
    <icon src="res/icon/windows-phone/icon-173.png" gap:platform="winphone"   gap:role="background" />-->
    <icon src="img/icons/apple-touch-icon.png" />
    <icon src="img/icons/apple-touch-icon-57x57-precomposed.png" gap:platform="ios" width="57" height="57"/>
    <icon src="img/icons/apple-touch-icon-72x72-precomposed.png" gap:platform="ios" width="72" height="72"/>
    <icon src="img/icons/apple-touch-icon-114x114-precomposed.png" gap:platform="ios" width="114" height="114"/>
    <icon src="img/icons/apple-touch-icon-144x144-precomposed.png" gap:platform="ios" width="144" height="144"/>
    <icon src="img/icons/apple-touch-icon-120x120-precomposed.png" gap:platform="ios" width="120" height="120"/>
    <icon src="img/icons/apple-touch-icon-76x76-precomposed.png" gap:platform="ios" width="76" height="76"/>
    <icon src="img/icons/apple-touch-icon-152x152-precomposed.png" gap:platform="ios" width="152" height="152"/>

    <!-- Define app splash screen for each platform. -->
    <!--  <gap:splash src="res/screen/android/screen-ldpi-portrait.png"  gap:platform="android" gap:density="ldpi" />
    <gap:splash src="res/screen/android/screen-mdpi-portrait.png"  gap:platform="android" gap:density="mdpi" />
    <gap:splash src="res/screen/android/screen-hdpi-portrait.png"  gap:platform="android" gap:density="hdpi" />
    <gap:splash src="res/screen/android/screen-xhdpi-portrait.png" gap:platform="android" gap:density="xhdpi" />
    <gap:splash src="res/screen/blackberry/screen-225.png"         gap:platform="blackberry" />
    <gap:splash src="res/screen/ios/screen-iphone-portrait.png"    gap:platform="ios"     width="320" height="480" />
    <gap:splash src="res/screen/ios/screen-iphone-portrait-2x.png" gap:platform="ios"     width="640" height="960" />
    <gap:splash src="res/screen/ios/screen-ipad-portrait.png"      gap:platform="ios"     width="768" height="1024" />
    <gap:splash src="res/screen/ios/screen-ipad-landscape.png"     gap:platform="ios"     width="1024" height="768" />
    <gap:splash src="res/screen/windows-phone/screen-portrait.jpg" gap:platform="winphone" />-->

    <!--
    Define access to external domains.

    <access />            - a blank access tag denies access to all external resources.
    <access origin="*" /> - a wildcard access tag allows access to all external resource.

    Otherwise, you can specify specific domains:
    -->
    <allow-navigation href="*"/>
    <allow-intent href="*"/>
    <access origin="*"/>
    <access origin="http://*.xxxxxxx/"/>
    <access origin="http://xxxxxx/"/>
    <!-- allow local pages -->
    <!--
    <access origin="http://phonegap.com" />                    - allow any secure requests to http://phonegap.com/
    <access origin="http://phonegap.com" subdomains="true" />  - same as above, but including subdomains, such as http://build.phonegap.com/
    -->

</widget>

这是通过 AJAX 抓取页面的脚本:

<script>
        var dev_mode = true;

        if (dev_mode) {
            alert('Development Mode Active - You will see popups used for debugging purposes.');
            var scripts_url = 'http://dev.xxxxxxx/app/scripts';
        } else {
            var scripts_url = 'http://xxxxxxxxx/app/scripts';
        }

        jQuery.ajax({
            type : "POST",
            url : scripts_url,
            success: function(html) {
                jQuery('head').append(html);
            },
            error: function(xhr, status, err) {
                if (dev_mode) {
                    alert('error getting scripts: ' + status);
                    alert('error getting scripts: ' + err);
                    alert('error getting scripts: ' + xhr.responseText);
                    alert('error getting scripts: ' + xhr.status);
                    alert('error getting scripts: ' + xhr.statusText);
                    alert('error getting scripts: ' + JSON.stringify(xhr));
                }
            }
        });
    </script>

AJAX 调用错误,状态为 0。

答案:

这是你的第一个应用吗?

不是我的第一个 PhoneGap 应用,而是我的第一个非常复杂的应用。

您的目标平台及其目标版本是什么?

针对除 Blackberry 之外的所有平台(在出现此 AJAX 错误之前已在 iOS、Android 和 WP 上进行测试)

您使用的是 CLI、SDK 还是 Build ?

构建

【问题讨论】:

  • 在您的帖子中回答以下问题。这是您的第一个应用程序吗?您的目标平台及其目标版本是什么?你在使用CLI, SDK or Build 吗?一旦您回答了帖子中的问题*,请在 cmets 中回复,以便我知道您已在帖子中添加信息。
  • 已答复。还添加了指向我发布此问题的其他地方的链接。
  • 您在哪个平台上遇到过问题?如果仅在 iOS 上,您是刚刚切换到 iOS 9 SDK 还是 iOS 9 设备?您是否添加了适当的应用程序传输安全设置?日志中显示了什么?
  • @BigBrother6 是的。我有同样的问题。重复什么是目标平台和它们的目标版本请回答。是的,这很重要——这就是我要问的原因。另外,您使用的是模拟器、网络浏览器还是设备。
  • 它适用于网络浏览器,但不适用于安卓设备。这是我在 Android 5.0.1 上遇到的一个全新问题。我想我主要针对的是最新版本的 iOS 和 Android。它已经工作了几个月,没有专门针对任何版本,我认为我没有做出任何会破坏它的更改。我非常感谢您的帮助,我只是不明白为什么特定的版本定位会产生很大的不同。再次感谢。

标签: javascript jquery ajax cordova phonegap-build


【解决方案1】:

不确定它是否有助于解决您的问题,但对于遇到类似问题的其他人,这可能会有所帮助:

几周前我遇到了完全相同的问题。 我的解决方案是将以下内容添加到我的服务器:

"Access-Control-Allow-Origin", "*";

"Access-Control-Allow-Headers", "X-Requested-With"

"Access-Control-Allow-Credentials", "X-Requested-With"

当我使用 Express (NODE) 服务器时,我通过添加 cors NPM 包来做到这一点。 然后,在服务器启动之前添加:

app.use(cors({credentials: true, origin: true}));

【讨论】:

    【解决方案2】:

    所有人注意:此应用使用不受限制的白名单过滤器,仅供开发使用。
    此应用示例不安全。保护您的应用程序由您决定。
    此外,您的应用可能会被 Google 和 Apple 拒绝。


    感谢大家的帮助,我很高兴地宣布我已经解决了这个问题。我不是 100% 确定是什么解决了这个问题,因为我尝试了许多解决方案,我怀疑这只是一切。然而,我第一次注意到代码实际工作是在我删除 config.xml 中的权限行时。

    总之,为了解决这个问题,我补充说:

    • CSP&lt;meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'"&gt;
    • 从 config.xml 中删除了权限和功能。 (新配置如下)
    • 在触发 AJAX 调用之前等待 deviceready 事件

    新的 Config.XML:

        <?xml version="1.0" encoding="UTF-8" ?>
        <widget xmlns   = "http://www.w3.org/ns/widgets"
            xmlns:gap   = "http://phonegap.com/ns/1.0"
            id          = "abc"
            version     = "1.0.0" >
    
        <name>abc</name>
        <description>abc</description>
        <author href="abc" email="abc">abc</author>
    
        <!-- basic config -->
    
        <preference name='phonegap-version' value='cli-5.2.0' />
        <preference name="android-build-tool" value="ant" />
    
        <preference name="orientation"                value="portrait" />
        <!-- all: default means both landscape and portrait are enabled -->
        <preference name="fullscreen"                 value="false" />
        <!-- all: hides the status bar at the top of the screen -->
        <preference name="target-device"              value="universal" />
        <!-- ios: possible values handset, tablet, or universal -->
        <preference name="prerendered-icon"           value="true" />
        <!-- ios: if icon is prerendered, iOS will not apply it's gloss to the app's icon on the user's home screen -->
        <preference name="ios-statusbarstyle"         value="black-opaque" />
        <!-- ios: black-translucent will appear black because the PhoneGap webview doesn't go beneath the status bar -->
        <preference name="detect-data-types"          value="true" />
        <!-- ios: controls whether data types (such as phone no. and dates) are automatically turned into links by the system -->
        <preference name="exit-on-suspend"            value="false" />
        <!-- ios: if set to false, the splash screen must be hidden using a JavaScript API -->
        <preference name="disable-cursor"             value="false" />
        <!-- blackberry: prevents a mouse-icon/cursor from being displayed on the app -->
    
    
        <!-- START Splash Screen Config -->
    
    
        <!--<preference name="auto-hide-splash-screen" value="false" />
        <preference name="SplashScreenDelay" value="500000" />-->
    
        <gap:splash src="splash.png" />
    
        <!-- iPhone and iPod touch -->
        <gap:splash src="img/splash/Default.png" gap:platform="ios" width="320" height="480" />
        <gap:splash src="img/splash/Default@2x.png" gap:platform="ios" width="640" height="960" />
    
        <!-- iPhone 5 / iPod Touch (5th Generation) -->
        <gap:splash src="img/splash/Default-568h@2x.png" gap:platform="ios" width="640" height="1136" />
    
        <!-- iPhone 6 -->
        <gap:splash src="img/splash/Default-667h@2x.png" gap:platform="ios" width="750" height="1334" />
        <gap:splash src="img/splash/Default-Portrait-736h@3x.png" gap:platform="ios" width="1242" height="2208" />
        <gap:splash src="img/splash/Default-Landscape-736h@3x.png" gap:platform="ios" width="2208" height="1242" />
    
        <!-- iPad -->
        <gap:splash src="img/splash/Default-Portrait.png" gap:platform="ios" width="768" height="1024" />
        <gap:splash src="img/splash/Default-Landscape.png" gap:platform="ios" width="1024" height="768" />
    
        <!-- Retina iPad -->
        <gap:splash src="img/splash/Default-Portrait@2x.png" gap:platform="ios" width="1536" height="2048" />
        <gap:splash src="img/splash/Default-Landscape@2x.png" gap:platform="ios" width="2048" height="1536" />
    
        <!-- Android -->
        <gap:splash src="img/splash/port-ldpi.png" gap:platform="android" gap:qualifier="port-ldpi" />
        <gap:splash src="img/splash/land-ldpi.png" gap:platform="android" gap:qualifier="land-ldpi" />
        <gap:splash src="img/splash/port-mdpi.png" gap:platform="android" gap:qualifier="port-mdpi" />
        <gap:splash src="img/splash/land-mdpi.png" gap:platform="android" gap:qualifier="land-mdpi" />
        <gap:splash src="img/splash/port-hdpi.png" gap:platform="android" gap:qualifier="port-hdpi" />
        <gap:splash src="img/splash/land-hdpi.png" gap:platform="android" gap:qualifier="land-hdpi" />
        <gap:splash src="img/splash/port-xhdpi.png" gap:platform="android" gap:qualifier="port-xhdpi" />
        <gap:splash src="img/splash/land-xhdpi.png" gap:platform="android" gap:qualifier="land-xhdpi" />
    
    
        <!-- END Splash Screen Config -->
    
        <!-- allow background music play iOS -->
        <gap:config-file platform="ios" parent="UIBackgroundModes" overwrite="true">
          <array>    
                 <string>audio</string>
          </array>
        </gap:config-file>
    <icon src="img/icons/apple-touch-icon.png" />
        <icon src="img/icons/apple-touch-icon-57x57-precomposed.png" gap:platform="ios" width="57" height="57"/>
        <icon src="img/icons/apple-touch-icon-72x72-precomposed.png" gap:platform="ios" width="72" height="72"/>
        <icon src="img/icons/apple-touch-icon-114x114-precomposed.png" gap:platform="ios" width="114" height="114"/>
        <icon src="img/icons/apple-touch-icon-144x144-precomposed.png" gap:platform="ios" width="144" height="144"/>
        <icon src="img/icons/apple-touch-icon-120x120-precomposed.png" gap:platform="ios" width="120" height="120"/>
        <icon src="img/icons/apple-touch-icon-76x76-precomposed.png" gap:platform="ios" width="76" height="76"/>
        <icon src="img/icons/apple-touch-icon-152x152-precomposed.png" gap:platform="ios" width="152" height="152"/>
    
        <gap:plugin name="cordova-plugin-device" source="npm"/>
        <gap:plugin name="cordova-plugin-file" source="npm"/>
        <gap:plugin name="cordova-plugin-media" source="npm"/>
        <gap:plugin name="cordova-plugin-splashscreen" source="npm"/>
        <gap:plugin name="cordova-plugin-whitelist" source="npm"/>
        <gap:plugin name="cordova-plugin-x-socialsharing" source="npm"/>
        <gap:plugin name="cordova-plugin-admobpro" source="npm" spec="2.9.6"/>
    
        <allow-navigation href="*"/>
        <allow-intent href="*"/>
        <access origin="*"/>
    
    </widget>
    

    新的 JS AJAX 调用:

    document.addEventListener("deviceready", on_device_ready, false);
                function on_device_ready() {
                    jQuery.ajax({
                        type : "GET",
                        url : scripts_url,
                        success: function(html) {
                            jQuery('head').append(html);
                        },
                        error: function(xhr, status, err) {
                            if (dev_mode) {
                                alert('error getting scripts: ' + status);
                                alert('error getting scripts: ' + err);
                                alert('error getting scripts: ' + xhr.responseText);
                                alert('error getting scripts: ' + xhr.status);
                                alert('error getting scripts: ' + xhr.statusText);
                                alert('error getting scripts: ' + JSON.stringify(xhr));
                            }
                        }
                    });
                }
    

    再次感谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多