我遇到了我的phonegap在Web浏览器以及移动浏览器上都能正常工作的问题,但是当与phonegap移动应用程序一起使用或构建apk文件时,出现了ajax错误。有什么问题,请帮助我我是phonegap的新手。顺便说一句,API是使用codeigniter rest API制成的。
phonegap的Config.xml文件
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.helloworld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>MONTEAPP</name>
<description>
Hello World sample application that responds to the deviceready event.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
PhoneGap Team
</author>
<content src="index.html" />
<preference name="DisallowOverscroll" value="true" />
<preference name="android-minSdkVersion" value="14" />
<plugin name="cordova-plugin-battery-status" source="npm" spec="~1.1.1" />
<plugin name="cordova-plugin-camera" source="npm" spec="~2.1.1" />
<plugin name="cordova-plugin-media-capture" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-console" source="npm" spec="~1.0.2" />
<plugin name="cordova-plugin-contacts" source="npm" spec="~2.0.1" />
<plugin name="cordova-plugin-device" source="npm" spec="~1.1.1" />
<plugin name="cordova-plugin-device-motion" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-device-orientation" source="npm" spec="~1.0.2" />
<plugin name="cordova-plugin-dialogs" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-file" source="npm" spec="~4.1.1" />
<plugin name="cordova-plugin-file-transfer" source="npm" spec="~1.5.0" />
<plugin name="cordova-plugin-geolocation" source="npm" spec="~2.1.0" />
<plugin name="cordova-plugin-globalization" source="npm" spec="~1.0.3" />
<plugin name="cordova-plugin-inappbrowser" source="npm" spec="~1.3.0" />
<plugin name="cordova-plugin-media" source="npm" spec="~2.2.0" />
<plugin name="cordova-plugin-network-information" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-splashscreen" source="npm" spec="~3.2.1" />
<plugin name="cordova-plugin-statusbar" source="npm" spec="~2.1.2" />
<plugin name="cordova-plugin-vibration" source="npm" spec="~2.1.0" />
<platform name="android">
<icon density="ldpi" src="www/res/icon/android/drawable-ldpi-icon.png" />
<icon density="mdpi" src="www/res/icon/android/drawable-mdpi-icon.png" />
<icon density="hdpi" src="www/res/icon/android/drawable-hdpi-icon.png" />
<icon density="xhdpi" src="www/res/icon/android/drawable-xhdpi-icon.png" />
<icon density="xxhdpi" src="www/res/icon/android/drawable-xxhdpi-icon.png" />
<icon density="xxxhdpi" src="www/res/icon/android/drawable-xxxhdpi-icon.png" />
<splash density="land-ldpi" src="www/res/screen/android/drawable-land-ldpi-screen.png" />
<splash density="land-mdpi" src="www/res/screen/android/drawable-land-mdpi-screen.png" />
<splash density="land-hdpi" src="www/res/screen/android/drawable-land-hdpi-screen.png" />
<splash density="land-xhdpi" src="www/res/screen/android/drawable-land-xhdpi-screen.png" />
<splash density="land-xxhdpi" src="www/res/screen/android/drawable-land-xxhdpi-screen.png" />
<splash density="land-xxxhdpi" src="www/res/screen/android/drawable-land-xxxhdpi-screen.png" />
<splash density="port-ldpi" src="www/res/screen/android/drawable-port-ldpi-screen.png" />
<splash density="port-mdpi" src="www/res/screen/android/drawable-port-mdpi-screen.png" />
<splash density="port-hdpi" src="www/res/screen/android/drawable-port-hdpi-screen.png" />
<splash density="port-xhdpi" src="www/res/screen/android/drawable-port-xhdpi-screen.png" />
<splash density="port-xxhdpi" src="www/res/screen/android/drawable-port-xxhdpi-screen.png" />
<splash density="port-xxxhdpi" src="www/res/screen/android/drawable-port-xxxhdpi-screen.png" />
</platform>
<platform name="ios">
<icon height="57" platform="ios" src="www/res/icon/ios/icon.png" width="57" />
<icon height="114" platform="ios" src="www/res/icon/ios/icon@2x.png" width="114" />
<icon height="40" platform="ios" src="www/res/icon/ios/icon-40.png" width="40" />
<icon height="80" platform="ios" src="www/res/icon/ios/icon-40@2x.png" width="80" />
<icon height="50" platform="ios" src="www/res/icon/ios/icon-50.png" width="50" />
<icon height="100" platform="ios" src="www/res/icon/ios/icon-50@2x.png" width="100" />
<icon height="60" platform="ios" src="www/res/icon/ios/icon-60.png" width="60" />
<icon height="120" platform="ios" src="www/res/icon/ios/icon-60@2x.png" width="120" />
<icon height="180" platform="ios" src="www/res/icon/ios/icon-60@3x.png" width="180" />
<icon height="72" platform="ios" src="www/res/icon/ios/icon-72.png" width="72" />
<icon height="144" platform="ios" src="www/res/icon/ios/icon-72@2x.png" width="144" />
<icon height="76" platform="ios" src="www/res/icon/ios/icon-76.png" width="76" />
<icon height="152" platform="ios" src="www/res/icon/ios/icon-76@2x.png" width="152" />
<icon height="29" platform="ios" src="www/res/icon/ios/icon-small.png" width="29" />
<icon height="58" platform="ios" src="www/res/icon/ios/icon-small@2x.png" width="58" />
<icon height="87" platform="ios" src="www/res/icon/ios/icon-small@3x.png" width="87" />
<splash height="1136" platform="ios" src="www/res/screen/ios/Default-568h@2x~iphone.png" width="640" />
<splash height="1334" platform="ios" src="www/res/screen/ios/Default-667h.png" width="750" />
<splash height="2208" platform="ios" src="www/res/screen/ios/Default-736h.png" width="1242" />
<splash height="1242" platform="ios" src="www/res/screen/ios/Default-Landscape-736h.png" width="2208" />
<splash height="1536" platform="ios" src="www/res/screen/ios/Default-Landscape@2x~ipad.png" width="2048" />
<splash height="768" platform="ios" src="www/res/screen/ios/Default-Landscape~ipad.png" width="1024" />
<splash height="2048" platform="ios" src="www/res/screen/ios/Default-Portrait@2x~ipad.png" width="1536" />
<splash height="1024" platform="ios" src="www/res/screen/ios/Default-Portrait~ipad.png" width="768" />
<splash height="960" platform="ios" src="www/res/screen/ios/Default@2x~iphone.png" width="640" />
<splash height="480" platform="ios" src="www/res/screen/ios/Default~iphone.png" width="320" />
</platform>
<platform name="wp8">
<icon height="99" platform="wp8" src="www/res/icon/wp8/ApplicationIcon.png" width="99" />
<icon height="159" platform="wp8" src="www/res/icon/wp8/Background.png" width="159" />
<splash height="1280" platform="wp8" src="www/res/screen/wp8/screen-portrait.jpg" width="768" />
</platform>
<platform name="windows">
<icon height="150" platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-100.png" width="150" />
<icon height="30" platform="windows" src="www/res/icon/windows/Square30x30Logo.scale-100.png" width="30" />
<icon height="50" platform="windows" src="www/res/icon/windows/StoreLogo.scale-100.png" width="50" />
<splash height="300" platform="windows" src="www/res/screen/windows/SplashScreen.scale-100.png" width="620" />
<icon height="120" platform="windows" src="www/res/icon/windows/StoreLogo.scale-240.png" width="120" />
<icon height="44" platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-100.png" width="44" />
<icon height="106" platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-240.png" width="106" />
<icon height="70" platform="windows" src="www/res/icon/windows/Square70x70Logo.scale-100.png" width="70" />
<icon height="71" platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-100.png" width="71" />
<icon height="170" platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-240.png" width="170" />
<icon height="360" platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-240.png" width="360" />
<icon height="310" platform="windows" src="www/res/icon/windows/Square310x310Logo.scale-100.png" width="310" />
<icon height="150" platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-100.png" width="310" />
<icon height="360" platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-240.png" width="744" />
<splash height="1920" platform="windows" src="www/res/screen/windows/SplashScreenPhone.scale-240.png" width="1152" />
</platform>
<!-- <access origin="http://127.0.0.1:8080*"/> --> <!-- allow local pages -->
<!-- <access origin="http://10.10.1.129*"/> --> <!-- allow requests from your server -->
<access origin="http://*" />
<access origin="https://*" />
<allow-navigation href="*" />
<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:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
<plugin name="cordova-plugin-whitelist" spec="~1.2.2" />
</widget>
phonegap的html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<!-- This is a wide open CSP declaration. To lock this down for production, see below. -->
<!-- <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" /> -->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<meta http-equiv="Content-Security-Policy"
content="default-src *;
style-src 'self' 'unsafe-inline' 'unsafe-eval';
script-src 'self' 'unsafe-inline' 'unsafe-eval';">
<!-- <link rel="stylesheet" type="text/css" href="css/index.css" /> -->
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css">
<!-- Material Design Bootstrap -->
<link rel="stylesheet" href="css/mdb.min.css">
<!--Alert ko lagi -->
<link rel="stylesheet" type="text/css" href="css/sweetalert.css"/>
<!--Jquery Mobile Css -->
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.5.css"/>
<title>Sign In</title>
</head>
<body>
<div class="container-fluid">
<div class="row mt-5">
<!--Grid column-->
<div class="col-md-4">
<section class="form-elegant">
<!--Section: Live preview-->
<section class="form-light">
<!--Form without header-->
<div class="card">
<div class="card-body mx-4">
<!--Header-->
<div class="text-center">
<h3 class="pink-text mb-5">
<strong>Sign in</strong>
</h3>
</div>
<!--Body-->
<!-- <form action="#"> -->
<div class="md-form">
<input name="username" type="text" id="username" class="form-control">
<label for="Form-email2">Username</label>
</div>
<div class="md-form pb-3">
<input name="password" type="password" id="password" class="form-control">
<label for="Form-pass2">Password</label>
</div>
<!--Grid row-->
<div class="row d-flex align-items-center mb-4">
<!--Grid column-->
<div class="col-md-3 col-md-6 text-center">
<a type="submit" id="btn_signin" class="btn btn-pink btn-block btn-rounded z-depth-1">Sign In</a>
</div>
<!--Grid column-->
</div>
<!--Grid row-->
<!-- </form> -->
<!--Grid column-->
<div class="col-md-6">
<p class="font-small grey-text d-flex justify-content-end">Forgot Password?
<a href="forget.html" class="blue-text ml-1"> Click Here.</a>
</p>
</div>
<!--Grid column-->
</div>
</div>
<!--/Form without header-->
</section>
<!--Section: Live preview-->
</section>
</div>
<!--Grid column-->
</div>
</div>
<!-- <div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div> -->
<!-- Script Section -->
<!-- JQuery -->
<script src="js/jquery-3.3.1.min.js"></script>
<!-- Bootstrap tooltips -->
<script type="text/javascript" src="js/popper.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script type="text/javascript" src="js/bootstrap.js"></script>
<!-- MDB core JavaScript -->
<script type="text/javascript" src="js/mdb.min.js"></script>
<!--Custom scripts-->
<script>
// SideNav Initialization
// $(".button-collapse").sideNav();
// var container = document.querySelector('.custom-scrollbar');
// Ps.initialize(container, {
// wheelSpeed: 2,
// wheelPropagation: true,
// minScrollbarLength: 20
// });
</script>
<script type="text/javascript" src="js/sweetalert.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<!-- <script type="text/javascript" src="js/main.js"></script> -->
<!-- Jquery Mobile -->
<script type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="js/corejs/login.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
与HTML文件链接的Javascript文件
// $( document ).bind( "mobileinit", function(){
// // Make your jQuery Mobile framework configuration changes here!
// $.mobile.allowCrossDomainPages = true;
// $.mobile.ajaxEnabled = true;
// $.mobile.pushStateEnabled = false;
// $.support.cors = true;
// });
$(document).ready(function()
{
// Make your jQuery Mobile framework configuration changes here!
$.mobile.allowCrossDomainPages = true;
$.mobile.ajaxEnabled = true;
$.mobile.pushStateEnabled = false;
$.support.cors = true;
$("#btn_signin").click(signinclickfunction);
}); //document ready
// $("#btn_signin").click(function()
function signinclickfunction()
{
var appid="764df61a8dcd29d91207056e44f77a4714a13688";
var username=$("#username").val();
var password=$("#password").val();
if(!username || !password)
{ event.preventDefault();
swal("Oops!", "Please Enter Valid Username And Password", "error");
// sweetAlert('Congratulations!', 'Your message has been successfully sent', 'success');
}
else
{
var dataString="username="+username+"&password="+password+"&appid="+appid;
$.ajax({
type: "GET",
url:"http://localhost:8080/monte_api_distributer/API_Login/checklogin",
data: dataString,
dataType:"json",
// contenttype: "application/json",
crossDomain: true,
cache: false,
success: function(data){
// var obj = JSON.parse(data);
if(data.status==200)
{
$.each(data.result, function(i, field){
if(field.access_type==2)
{
//Teacher le login gareko
storeTeacherDataInStorage(field.id);
}
else if(field.access_type==3)
{
//Parents or Student le login gareko
}
});
}
else if (data.status==404)
{
event.preventDefault();
swal("Oops!", "Incorrect Username Or Password", "error");
// window.location.href="login.html";
}
else if (data.status==401)
{
event.preventDefault();
swal("Oops!", "Invalid Request. Please Try Again Later", "error");
// window.location.href="login.html";
}
else
{
event.preventDefault();
swal("Oops!", "Unknown Error Occured. Please Try Again Later", "error");
window.location.href="login.html";
}
},
error:function (error){
// console.log(error);
// swal("Oops!", error, "error");
// event.preventDefault();
swal("Oops!", "There was an error processing your request. Please try again.", "error");
// window.location.href="login.html";
}
});
}
}//submit click
function storeTeacherDataInStorage(loginid)
{
var appid="764df61a8dcd29d91207056e44f77a4714a13688";
var dataString="appid="+appid+"&loginid="+loginid;
$.ajax({
type: "GET",url:"http://localhost:8080/monte_api_distributer/API_Login/getTeacherInfo",
data: dataString,
dataType:"json",
contenttype: "application/json",
crossDomain: true,
cache: false,
success: function(data){
if(data.status==401)
{
event.preventDefault();
swal("Oops!", "Invalid Request. Please Try Again Later", "error");
// window.location.href="login.html";
}
else if(data.status==404)
{
event.preventDefault();
swal("Oops!", "Incorrect Credentials", "error");
// window.location.href="login.html";
}
else if(data.status==200)
{
$.each(data.result, function(i, field){
localStorage.setItem("loggedin","true");
localStorage.setItem("usertype","teacher");
localStorage.setItem("fullname",field.full_name);
localStorage.setItem("userid",field.id);
// console.log(localStorage.getItem("userid"));
window.location.href="teacherfeedspage.html";
});
}
else
{
event.preventDefault();
swal("Oops!", "Unknown Error Occured. Please Try Again Later", "error");
window.location.href="login.html";
}
},
error:function (error){
console.log(error);
event.preventDefault();
swal("Oops!", "There was an error processing your request. Please try again.", "error");
window.location.href="login.html";
}
});
} //storing teacher data
使用Codeigniter制作的REST API
require(APPPATH.'/libraries/REST_Controller.php');
// header("Access-Control-Allow-Methods: GET, OPTIONS");
// header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
class API_Login extends REST_Controller{
public function __construct($config = 'rest')
{
// $this->config->load('rest');
// header('Access-Control-Allow-Origin: *');
// header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
//
// header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
//
// $method = $_SERVER['REQUEST_METHOD'];
// if($method == "OPTIONS") {
// die();
// }
parent::__construct($config);
$this->load->model('Login_model');
}
//API - app sends uname and password and on valid login, information is sent back
function checklogin_get(){
$appid=$this->get('appid');
$uname = $this->get('username');
$pwd=md5($this->get('password'));
if($appid=="764df61a8dcd29d91207056e44f77a4714a13688")
{
if(!$uname || !$pwd){
// $this->response("No Username Or Password Specified", 400);
//EMPTY SENT
echo json_encode(array("status"=>400,"result"=>'none'));
exit;
}
$result = $this->Login_model->check_valid_login( $uname,$pwd );
if($result){
// $this->response($result, 200);
// RESULT OK
echo json_encode(array("status"=>200,"result"=>$result));
exit;
}
else{
// $this->response("Invalid ISBN", 404);
//INVALID PARAMETERS
echo json_encode(array("status"=>404,"result"=>'none'));
exit;
}
}
else
{
// $this->response("Invalid API Key",404);
//INVALID API KEY
echo json_encode(array("status"=>401,"result"=>'none'));
exit;
}
}
从Postman正常调用API
在计算机Web浏览器和移动Web浏览器上的APP测试正常
问题是-PHONEGAP移动应用程序中的测试应用程序会抛出ajax错误,以及生成APK文件,测试也会产生相同的ajax错误。 (程序流永远不会达到AJAX成功。)