Phonegap AJAX URL 在 android 设备上显示状态 404
Phonegap AJAX URL shows Status 404 on android device
我是 Phonegap/Cordova
的新人。我遇到了 2 天无法在我的移动设备上调用 AJAX
(URL) 的问题。我已经尝试了一切,阅读了每个论坛,但没有任何效果。在移动设备上,它总是显示“Status 404
”。它在 Ripple 模拟器上完美运行。即使我在许多论坛上阅读时也使用 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 上但仍然无法正常工作。感谢任何帮助。
经过2天的努力,在上面不断回复的@QuickFix的帮助下,问题已经解决
修复:
删除 'cordova-plugin-whitelist' 插件。
添加'cordova-plugin-legacy-whitelist'插件。
我是 Phonegap/Cordova
的新人。我遇到了 2 天无法在我的移动设备上调用 AJAX
(URL) 的问题。我已经尝试了一切,阅读了每个论坛,但没有任何效果。在移动设备上,它总是显示“Status 404
”。它在 Ripple 模拟器上完美运行。即使我在许多论坛上阅读时也使用 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 上但仍然无法正常工作。感谢任何帮助。
经过2天的努力,在上面不断回复的@QuickFix的帮助下,问题已经解决
修复:
删除 'cordova-plugin-whitelist' 插件。
添加'cordova-plugin-legacy-whitelist'插件。