2016-02-23 4 views
0

Я пытаюсь сделать http-вызов в мобильном приложении с использованием ионного фрейма.не удалось выполнить http-вызов

Ниже мой интерфейс:

<ion-view title="Register"> 
    <ion-content overflow-scroll="true" padding="true" scroll="false" class="has-header has-footer" ng-controller="registerCtrl"> 
     <form class="list" name="regform" novalidate> 
      <ion-list> 
       <label class="item item-input" name="Number"> 
        <span class="input-label">Number</span> 
        <input type="tel" name="num" placeholder="" ng-model="user.ph_no" ng-minlength="10" ng-maxlength="10" required> 
       </label> 
       <label class="item item-input" name="Email"> 
        <span class="input-label">Email</span> 
        <input type="email" name="email" placeholder="" ng-model="user.email" ng-minlength="6" required> 
       </label> 
       <label class="item item-input" name="Password"> 
        <span class="input-label">Password</span> 
        <input type="password" name="password" placeholder="" ng-model="user.password" ng-minlength="6" required> 
       </label> 
      </ion-list> 
      <div class="padding"> 
       <p ng-show="regform.num.$error.required">* Phone Number is required</p> 
       <p ng-show="regform.num.$error.minlength">* Phone Number should be 10 digits</p> 
       <p ng-show="regform.num.$error.maxlength">* Phone Number should be 10 digits</p> 
       <p ng-show="regform.email.$error.required">* Email is required</p> 
       <p ng-show="regform.email.$error.minlength">* Email must be longer than 5 letters</p> 
       <p ng-show="regform.password.$error.required">* Password is required</p> 
       <p ng-show="regform.password.$error.minlength">* Password must be longer than 6 characters</p> 
      </div> 
      <ion-checkbox name="checkbox" ng-model="checked">I Agree with T&amp;C</ion-checkbox> 
      <a ng-click="proceed()" class="button button-block button-positive" ng-disabled="!checked || regform.$invalid">Proceed</a> 
     </form> 
    </ion-content> 
</ion-view> 

Я зарегистрировал службу HTTPService в app.js файле.

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives']) 

.run(function($ionicPlatform) { 
    $ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
    if(window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
    } 
    if(window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleDefault(); 
    } 
    }); 
}) 

.service('HttpService', function($http) { 
return { 
    getPost: function() { 
    // $http returns a promise, which has a then function, which also returns a promise. 
    return $http.get('http://jsonplaceholder.typicode.com/posts/1') 
     .then(function (response) { 
     // In the response, resp.data contains the result. Check the console to see all of the data returned. 
     console.log('Get Post', response); 
     return response.data; 
     }); 
    } 
}; 
}); 

В контроллер т.е. controllers.js, я использую в нг щелкните событие:

angular.module('app.controllers', ['ionic']) 

.controller('registerCtrl', function($scope,backcallFactory, HttpService, $ionicLoading) { 

    backcallFactory.backcallfun(); 
    $scope.user = {}; 

    $scope.proceed = function() { 
     $ionicLoading.show({ 
      template: 'Loading...' 
     }); 

     HttpService.getPost() 
      .then(function(response) { 
       $scope.user.ph_no = response; 
       $ionicLoading.hide(); 
      }); 

    }; 
}) 

.factory('backcallFactory', ['$state','$ionicPlatform','$ionicHistory','$timeout',function($state,$ionicPlatform,$ionicHistory,$timeout){ 

     var obj={} 
     obj.backcallfun=function(){ 
     var backbutton=0; 

      $ionicPlatform.registerBackButtonAction(function() { 
       if ($state.current.name == "register") { 
        /*var action= confirm("Do you want to Exit?"); 
        if(action){ 
         navigator.app.exitApp(); 
        }//no else here just if*/ 

        if(backbutton==0){ 
         backbutton++; 
         window.plugins.toast.showShortCenter('Press again to exit'); 
         $timeout(function(){backbutton=0;},5000); 
        }else{ 
         navigator.app.exitApp(); 
        } 

       } 
       else{ 

         $ionicHistory.goBack(); 
         /*$ionicHistory.nextViewOptions({ 
         disableBack: true 
         }); 
         $state.go('app.home');*/ 
         //go to home page 
        } 
      }, 100);//registerBackButton 
     }//backcallfun 
     return obj; 
    }]) 

Проблема заключается в том, когда я нажимаю кнопку продолжить его stucked на ионном экран загрузки.

enter image description here

Я буквально с помощью учебника http://appcamp.io/courses/networking/fetching-data-api, но с в случае нажатия кнопки.

Может кто-то сказать, что мне не хватает.

+0

Вы проверили свой api с человеком, занимающимся приложением Google? –

ответ

0

Проверьте версию кордовы-андроида, которую вы используете. Если это версия 4, вам нужен плагин с белым списком, чтобы получить «HTTP» -выполнения в приложениях.

Проверить это,

Cordova/Ionic : $http request not processing while emulating or running on device

ваша проблема все вы запрашиваете возвращают 404 и ошибки не обрабатываются хорошо, попробуйте использовать обещания от службы $ Q, чтобы поймать ошибки правильно.

Смежные вопросы