Ionic $ http给出null响应

时间:2016-05-11 14:07:01

标签: android cordova ionic-framework whitelist

我也面临同样的问题。无法解决问题。我正在使用以下版本的离子和cordova。 离子:1.7.14 cordova:6.1.1

我已在我的应用中安装了whitelist插件,并在config.xml中添加了以下行。

还尝试删除android平台。但问题仍然存在。

请建议。欢迎提出所有建议。

这是我的index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title></title>
        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
        <link href="lib/font-awesome-4.5.0/css/font-awesome.css" rel="stylesheet">
      <link href="css/ionic.app.css" rel="stylesheet">
        <script src="lib/ionic/js/ionic.bundle.js"></script>
        <!-- cordova script (this will be a 404 during development) -->
        <script src="cordova.js"></script>
        <script src="lib/ng-cordova.min.js"></script>

        <!-- your app's js -->
        <script src="lib/angularLocalStorage/angular-local-storage.js"></script>
        
        <script src="js/app.js"></script>
        <script src="js/app.config.js"></script>
        <script src="js/app.constants.js"></script>
        <script src="js/app.controllers.js"></script>
        <script src="js/app.services.js"></script>

    </head>

    <body ng-controller="AppCtrl">
        <ion-nav-bar class="bar-positive">

        </ion-nav-bar>

        <!--navigation view-->
        <ion-nav-view>
        </ion-nav-view>
    </body>
</html>

function  createHttpRequestObject = function (url, data, method, headers) {
            var req = {};
            req['method'] = method || 'POST';
            if (action) {
                req['url'] = url;
            }
            if (data) {                
                req['data'] = data;
            }
            req['headers'] = headers || { 'Content-Type': 'application/x-www-form-urlencoded' };
            return req;
        }


var deferred = $q.defer();
$http(createHttpRequestObject('my-url.com/postData', data)) .success(function (res) {
})
.error(function (data, status, headers, config) { 
 alert(data)
});

1 个答案:

答案 0 :(得分:0)

    // Simple GET request example:
    $http({
      method: 'POST',
      url: 'my-url.com/postData',
      data: yourDataObject,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      }
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });
  

$ http遗留承诺方法成功与错误   弃用。

src:https://docs.angularjs.org/api/ng/service/ $ http