Ajax Call在使用IONIC的移动设备中无效

时间:2016-07-07 17:35:32

标签: javascript cordova ionic-framework

我已经创建了离子应用程序,我在浏览器中运行应用程序工作正常。但我尝试在移动设备上运行的同一个应用程序AJAX调用不起作用。在ajax我发出警报之前,它已被填充。以下是我的代码。

代码是,

$http({
        method: 'POST', 
        //url: syncURL,
        url:"http://45.xx.xxx:8080/MobileSync/SummaTest/update_SummaNoun",
        data: surveyResultrecords,//surveyResultrecords 
         }).then(function(result){
                            console.log( "yay---" +JSON.stringify(result.data));
                            alert("yay");
                            alert("APTAM");
                                            //$scope.SummaNoun.id = surveyResultrecords.id;  
                                            //var promise1 = imageUpload(); 
                                            //console.log("PUSH completed ");
                                            //alert("test---finished");
                                            return "data Updated";//imageUpload();

                        }).then(function(ress){
                                console.log("PUSH completed ");
                                alert("push completed")
                        });

这些是我安装的cordova插件,

"cordovaPlugins": [
    "cordova-plugin-camera",
    "cordova-plugin-device",
    "cordova-plugin-console",
    "cordova-plugin-splashscreen",
    "cordova-plugin-statusbar",
    "ionic-plugin-keyboard",
    "cordova-plugin-whitelist",
    "cordova-plugin-file",
    "cordova-plugin-file-transfer"
  ]

的Config.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.Testcamera10008" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>testCamera</name>
  <description>
        An Ionic Framework and Cordova project.
    </description>
  <author email="you@example.com" href="http://example.com.com/">
      Your Name Here
    </author>
  <content src="index.html"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="UIWebViewBounce" value="false"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="SplashScreenDelay" value="2000"/>
  <preference name="FadeSplashScreenDuration" value="2000"/>
  <preference name="android-minSdkVersion" value="16"/>
  <preference name="BackupWebStorage" value="none"/>
  <preference name="SplashScreen" value="screen"/>
  <feature name="StatusBar">
    <param name="ios-package" value="CDVStatusBar" onload="true"/>
  </feature>
  <platform name="android">
    <allow-intent href="market:*"/>
    <allow-intent href="*"/>
    <allow-navigation href="*"/>
    <access origin="*"/>
    <icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/>
    <icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/>
    <icon src="resources/android/icon/drawable-hdpi-icon.png" density="hdpi"/>
    <icon src="resources/android/icon/drawable-xhdpi-icon.png" density="xhdpi"/>
    <icon src="resources/android/icon/drawable-xxhdpi-icon.png" density="xxhdpi"/>
    <icon src="resources/android/icon/drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
    <splash src="resources/android/splash/drawable-land-ldpi-screen.png" density="land-ldpi"/>
    <splash src="resources/android/splash/drawable-land-mdpi-screen.png" density="land-mdpi"/>
    <splash src="resources/android/splash/drawable-land-hdpi-screen.png" density="land-hdpi"/>
    <splash src="resources/android/splash/drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/>
    <splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/>
    <splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/>
    <splash src="resources/android/splash/drawable-port-hdpi-screen.png" density="port-hdpi"/>
    <splash src="resources/android/splash/drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
  </platform>
</widget>

的index.html

<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <meta http-equiv="Content-Security-Policy">
    <title>Testcamera</title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">

    <script src="lib/ionic/js/ionic.bundle.js"></script><script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
    <script src="cordova.js"></script>
    <script src="app/js/app.js"></script>
    <script src="lib/jquery/jquery-2.1.3.min.js"></script>
    <script src="lib/ionic-datepicker/dist/ionic-datepicker.bundle.min.js"></script>
    <script src="app/js/controllers/AplicationLevelCtrl.js"></script>
    <script src="app/js/controllers/SummaTest.js"></script>
    <script src="app/js/controllers/SummaNoun_Default_Activity.js"></script>

<body ng-app="Testcamera">
    <ion-nav-view></ion-nav-view>
</html>

Chrome Inspecter Image Chrome Inspecter for that ajax call worked in Browser for your reference

我安装了cordova-whitelist插件,也是我的cordova版本6.2.0,离子1.7.16。

4 个答案:

答案 0 :(得分:0)

为什么在config.xml中没有插件声明,例如:

<plugin name="cordova-plugin-whitelist" spec="~1.2.2" />

你没有这个标签:

<access origin="*" subdomains="true" />

read here了解更多关于它的信息

此外,post可能会对您有所帮助。祝你好运

答案 1 :(得分:0)

首先,您的代码中不需要额外的then,因为它没有返回承诺。它只是一个字符串文字。始终处理承诺的错误部分。

更好的版本:

$http.post(url, data)
  .then(function (res){ // handles response
    console.log(res.data); // or execute success function
  }, function (err){ // handles error
    console.log(err); // or JSON.stringify(err) if it shows [object] on the console
  });

确保设备上platforms/(android/ios)上的所有插件都在那里。

之后,执行ionic run android/ios --lc并在命令行上查看错误日志以查看出现了什么问题。

答案 2 :(得分:0)

这可能是白名单上的问题。这就是AJAX调用可能在浏览器中而不是在设备中正常工作的原因。你必须在config.xml中使用allow-intent属性和HTTP和HTTPS协议的适当设置来摆脱这个问题。

建议您查看具有所需设置的SO Post以解决此问题

答案 3 :(得分:0)

感谢所有善意回复的人,我们发现问题跟随@Italo Ayres他的评论远程调试Android设备。我们看到了应用程序的日志并更正了错误。

因为我们尝试使用Android Studio查看日志,但它没有显示Ajax Call it仅在AJAX调用日志之前显示。

我们推荐此链接使用Chrome查看移动日志。请查看。它非常实用。 Remote Debugging Android Devices