【发布时间】:2015-06-29 11:05:16
【问题描述】:
我是Phonegap/Cordova 的新手。我面临 2 天的问题,无法在移动设备上拨打 AJAX (URL)。我已经尝试了所有事情,阅读了每个论坛,但没有任何效果。在移动设备上,它总是显示“Status 404”。它在 Ripple Emulator 上完美运行。即使我在许多论坛上阅读时都使用 promise.js。下面是代码sn-p:
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 天遇到此问题的解决方案吗? 我还在我的安卓设备上打开了移动互联网,但没有任何效果。首先,我现在在 URL 中使用我的本地网络 IP,我部署在 Internet 上但仍然无法正常工作。任何帮助表示赞赏。
【问题讨论】:
-
您是否安装并配置了插件 cordova-plugin-whitelist ? (使用cordova 5创建项目时应该自动添加)
-
是的,我安装了cordova-plugin-whitelist。
-
你已经完全配置好了(在 config.xml 和 html 文件中?)
-
查看此链接和插件文档:raymondcamden.com/2015/05/25/…
-
是的,config.xml 附在上面。请检查它是否满足白名单要求。
标签: javascript android jquery cordova phonegap-plugins