Phonegap AJAX URL在Android设备上显示状态404

时间:2015-06-29 11:05:17

标签: javascript android jquery cordova phonegap-plugins

我是Phonegap/Cordova的新人。我在2天内遇到问题我无法在移动设备上拨打AJAX(网址)。我已经尝试了所有的东西,阅读每个论坛但没有任何作用。在移动设备上,它始终显示“Status 404”。它在Ripple Emulator上完美运行。我在许多论坛上阅读时甚至使用了promise.js。以下是代码段:

 var app = {
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {

    $(document).ready(function () {
        var pageData;

        $('#test-form').submit(function(event) {
            event.preventDefault();
            var formVal1 = $('#formVal1').val().trim();
            var formVal2 = $('#formVal2').val().trim();

            if (formVal1 != '' && formVal2 != '')    {
                // B-> Here you define its functions and its payload
                var mdnAPI = 'https://developer.mozilla.org/en-US/search.json';
                /*var payload = {
                    'formVal1' : formVal1,
                    'formVal2' : formVal2
                };*/
                var payload = {
                    'topic' : 'js',
                    'q'     : 'Promise'
                }

                var callback = {
                    success : function(data) {
                    console.log(1, 'success', JSON.parse(data));
                    /* $.mobile.changePage("#application-status", {
                        respData: JSON.parse(data),
                        transition: "flip"
                     }); */
                  },
                  error : function(data) {
                     console.log(2, 'error', JSON.parse(data));
                  }
                };

                $.support.cors=true;
                // Executes the method call
                $http(mdnAPI)
                    .post(payload)
                    .then(callback.success)
                    .catch(callback.error);
            }
            return false;       //this is must
        });
    }
  }

AndroidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.project.sample.buildingpermit" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="22" />

Config.xml

  <access origin="*" />
  <access origin="https://developer.mozilla.org/en-US/search.json" />

index.html

    <script src="cordova.js"></script>
    <script src="js/index.js"></script>
    <script>
        $(document).bind( "mobileinit", function() {
        // Make your jQuery Mobile framework configuration changes here!
        alert('I am here ');
        $.mobile.allowCrossDomainPages = true;
        });
    </script>
    <script type="text/javascript">
        $.support.cors=true;
        app.initialize();
    </script>

注意:我执行命令:

1)cordova平台版android显示:已安装的平台:android 4.0.2

2)phonegap -v显示:5.0.0-0.28.1

任何人都能为我提供解决方案,我在过去2天内遇到了这个问题吗? 我也在我的Android设备上打开移动互联网,但没有任何作用。首先,我现在在URL中使用我的本地网络IP,我在Internet上部署但仍然无法正常工作。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

经过2天的艰苦努力,在@QuickFix的帮助下,上面不断回答,问题已经解决了。

修正:

  • 删除'cordova-plugin-whitelist'插件。

  • 添加'cordova-plugin-legacy-whitelist'插件。