$http 调用不适用于 Ionic Android 构建

$http call won't work on Ionic Android build

我是这样打电话的:

$http.get( url, {
    params : {
        empId: $scope.empId
    }
}).then(function(data, status){
    $scope.workOrders = data.data;
}, function(data, status){
    $scope.message = data;
});

它在 Chrome 上工作得很好,如果我在我的 phones 浏览器上导航到 URL,我可以得到很好的响应。

但是,每当我使用构建的 .apk 时,我都会得到:

{"data":"",
 "status":404,
 "config":{
     "method":"GET",
     "transformRequest":[null],
     "transformResponse":[null],
     "params":{"empId":"123"},
     "url":"http://...",
     "headers":{"Accept":"application/json, text/plain, */*"}
 },"statusText":Not Found"}

有点迷失在这一点上。奇怪的是,我可以从我的 phone 浏览器中点击 URL,但不能在 Ionic/Cordova 内置的 .apk

中点击

根据要求,这是我的 config.xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.myapp397384" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>myApp</name>
  <description>
        An Ionic Framework and Cordova project.
    </description>
  <author email="hi@ionicframework" href="http://ionicframework.com/">
      Ionic Framework Team
    </author>
  <content src="index.html"/>
  <access origin="*"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="UIWebViewBounce" value="false"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="BackupWebStorage" value="none"/>
  <feature name="StatusBar">
    <param name="ios-package" value="CDVStatusBar" onload="true"/>
  </feature>
</widget>

啊哈!

显然 Cordova 不久前刚刚发布了 cordova-android 4.0,它在默认情况下会阻止 http 请求。

只需 运行 这个命令:

ionic plugin add cordova-plugin-whitelist

你可以开始了。