2016-01-10 58 views
0

我正在使用Phonegap-CLI(v5.4.0)和Phonegap-developer-app(Android和iOS,都是最新的)创建一个简单的跨平台应用程序。在我的应用程序中,我试图调用我实现的REST接口,我使用jQuery AJAX来实现。不幸的是,这些通话似乎不起作用,我的控制台中没有看到任何来电。 (REST接口在我的机器上本地运行。)但是,我知道我的REST接口工作正常,因为我可以使用IntelliJ-testing-tool正确调用REST接口。Phonegap似乎阻止网络连接

index.html看起来是这样的:

<html> 
    <head> 
     <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"> 
     <meta charset="utf-8" /> 
     <meta name="format-detection" content="telephone=no" /> 
     <meta name="msapplication-tap-highlight" content="no" /> 
     <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
     <link rel="stylesheet" type="text/css" href="css/index.css" /> 

     <!-- Bootstrap --> 
     <!-- Latest minified version --> 
     <link rel="stylesheet" href="css/bootstrap.min.css"></link> 
     <!--Optional theme--> 
     <link rel="stylesheet" href="css/bootstrap-theme.min.css"></link> 
     <title>MyApp</title> 
    </head> 
    <body class="noscroll"> 

     <div class="app"> 

      <!-- ... --> 
      <div class="row fullWidth" style="height:70px;"> 
       <div class="col-xs-1"></div> 
       <div class="col-xs-10"><button id="logInBtn" class="fillParent emphSmall">Login</button></div> 
       <div class="col-xs-1"></div> 
      </div> 

     </div> 

     <script type="text/javascript" src="cordova.js"></script> 
     <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
     <script type="text/javascript" src="js/customFunctions.js"></script> 
     <script type="text/javascript"> 

     document.addEventListener("deviceready", function(){ 

      $("#logInBtn").on("click", function(){ 
       alert("beginning ajax call"); 

       $.ajax({ 
        type: "GET", 
        cache: false, 
        Accept: "application/json; charset=utf-8", 
        url: "http://<ip>:8080/myApp/rest/Something", 
        success: function(){ 
         alert("works!") 

        }, 
        error: function(){ 
         alert("doesn't work!"); 
        } 
       }); 

      }); 

     }, true); 

     /*app.initialize();*/ 
     </script> 
    </body> 
</html> 

这是我​​3210的样子。这是这是在创建项目所产生的默认文件:

<?xml version='1.0' encoding='utf-8'?> 
<widget id="my.app.identifier" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0"> 
<name>hibou</name> 
<description> 
    Hello World sample application that responds to the deviceready event. 
</description> 
<author email="[email protected]" href="http://phonegap.com"> 
    PhoneGap Team 
</author> 
<content src="index.html" /> 
<preference name="permissions" value="none" /> 
<preference name="orientation" value="default" /> 
<preference name="target-device" value="universal" /> 
<preference name="fullscreen" value="true" /> 
<preference name="webviewbounce" value="true" /> 
<preference name="prerendered-icon" value="true" /> 
<preference name="stay-in-webview" value="false" /> 
<preference name="ios-statusbarstyle" value="black-opaque" /> 
<preference name="detect-data-types" value="true" /> 
<preference name="exit-on-suspend" value="false" /> 
<preference name="show-splash-screen-spinner" value="true" /> 
<preference name="auto-hide-splash-screen" value="true" /> 
<preference name="disable-cursor" value="false" /> 
<preference name="android-minSdkVersion" value="14" /> 
<preference name="android-installLocation" value="auto" /> 
<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" /> 
<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" /> 
<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.media" /> 
<gap:plugin name="org.apache.cordova.network-information" /> 
<gap:plugin name="org.apache.cordova.splashscreen" /> 
<gap:plugin name="org.apache.cordova.vibration" /> 
<icon src="icon.png" /> 
<icon gap:platform="android" gap:qualifier="ldpi" src="www/res/icon/android/icon-36-ldpi.png" /> 
<icon gap:platform="android" gap:qualifier="mdpi" src="www/res/icon/android/icon-48-mdpi.png" /> 
<icon gap:platform="android" gap:qualifier="hdpi" src="www/res/icon/android/icon-72-hdpi.png" /> 
<icon gap:platform="android" gap:qualifier="xhdpi" src="www/res/icon/android/icon-96-xhdpi.png" /> 
<icon gap:platform="blackberry" src="www/res/icon/blackberry/icon-80.png" /> 
<icon gap:platform="blackberry" gap:state="hover" src="www/res/icon/blackberry/icon-80.png" /> 
<icon gap:platform="ios" height="57" src="www/res/icon/ios/icon-57.png" width="57" /> 
<icon gap:platform="ios" height="72" src="www/res/icon/ios/icon-72.png" width="72" /> 
<icon gap:platform="ios" height="114" src="www/res/icon/ios/icon-57-2x.png" width="114" /> 
<icon gap:platform="ios" height="144" src="www/res/icon/ios/icon-72-2x.png" width="144" /> 
<icon gap:platform="webos" src="www/res/icon/webos/icon-64.png" /> 
<icon gap:platform="winphone" src="www/res/icon/windows-phone/icon-48.png" /> 
<icon gap:platform="winphone" gap:role="background" src="www/res/icon/windows-phone/icon-173-tile.png" /> 
<gap:splash gap:platform="android" gap:qualifier="port-ldpi" src="www/res/screen/android/screen-ldpi-portrait.png" /> 
<gap:splash gap:platform="android" gap:qualifier="port-mdpi" src="www/res/screen/android/screen-mdpi-portrait.png" /> 
<gap:splash gap:platform="android" gap:qualifier="port-hdpi" src="www/res/screen/android/screen-hdpi-portrait.png" /> 
<gap:splash gap:platform="android" gap:qualifier="port-xhdpi" src="www/res/screen/android/screen-xhdpi-portrait.png" /> 
<gap:splash gap:platform="blackberry" src="www/res/screen/blackberry/screen-225.png" /> 
<gap:splash gap:platform="ios" height="480" src="www/res/screen/ios/screen-iphone-portrait.png" width="320" /> 
<gap:splash gap:platform="ios" height="960" src="www/res/screen/ios/screen-iphone-portrait-2x.png" width="640" /> 
<gap:splash gap:platform="ios" height="1136" src="www/res/screen/ios/screen-iphone-portrait-568h-2x.png" width="640" /> 
<gap:splash gap:platform="ios" height="1024" src="www/res/screen/ios/screen-ipad-portrait.png" width="768" /> 
<gap:splash gap:platform="ios" height="768" src="www/res/screen/ios/screen-ipad-landscape.png" width="1024" /> 
<gap:splash gap:platform="winphone" src="www/res/screen/windows-phone/screen-portrait.jpg" /> 
<plugin name="cordova-plugin-whitelist" version="1" /> 
<access origin="*" /> 
<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> 

通过以上的设置,我总是得到“开始”和“不工作” -alert。但是因为当我将它包含到非phonePhone HTML文档中时,AJAX调用可以正常工作,并且来自我的应用程序的调用甚至不会到达我的REST接口,所以我认为它可能会带来一些问题phonegap-whitelist-plugin,但正如您所看到的,它已包含在​​3210中,并且<access origin="*" />标签也应允许所有网络连接。我已经检查过安全策略元标记,但据我所知,这也不应该导致这个问题。毕竟,我不知道什么可能会导致这个错误,所以我非常感谢一些帮助或暗示,我可能会错过这里... 非常感谢!

回答