简体   繁体   English

ionic http到达本地文件会给出http错误代码0?

[英]ionic http get to a local file gives http error code 0?

I'm building a map using leaflet, mapbox, and omnivore. 我正在使用传单,地图框和杂食动物构建地图。 I have a .kml file with a list of markers on it. 我有一个.kml文件,上面带有标记列表。 The markers load fine on desktop using ionic serve, but not when I actually run it on a mobile device (I have tested both android/ios). 使用离子服务在桌面上可以很好地加载标记,但是当我实际上在移动设备上运行它时(我已经测试了两个android / ios),标记不能加载。

I know that omnivore loads this file using an xhr request, so I decided to test that myself: 我知道omnivore使用xhr请求加载了此文件,因此我决定自己进行测试:

test = function() {
      $http.get('/kml/file.kml').success(function(data, status, headers, config) {
        alert('Success!: ' + status);
      })
        .error(function(data, status,headers,config) {
          alert('FAILURE!: ' + status);
        });
    }

I get 200 success on desktop, but when I run it on a mobile device using ionic view or build it for Android I get failure with error code 0. 我在台式机上获得了200成功,但是当我使用ionic view在移动设备上运行它或为Android构建它时,我得到了失败,错误代码为0。

I'm not sure why this is happening on mobile devices, I have <access origin="*"/> in my config.xml. 我不确定为什么在移动设备上会发生这种情况,我在config.xml中有<access origin="*"/>

Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!

Dont't know what version of Angular you are running, but $http.success and $http.error has been deprecated (since v.1.4.4 I think) and been replaced with success and error callback functions 不知道您正在运行什么版本的Angular,但是$http.success$http.error弃用 (我认为自v.1.4.4起),并已替换为成功和错误回调函数

$http.get('/kml/file.kml').then(
  function successCallback(response) {
     console.log(response);
  }, function errorCallback(error) {
     console.log(error);
});

Not sure if this is your problem, but try update your angular and/or change your code and see if this makes any difference. 不确定这是否是您的问题,但是请尝试更新角度和/或更改代码,看看是否有任何区别。

Well, I figured out the solution. 好吧,我想出了解决方案。 I had a leading slash in the url to the file which was being read correctly when ionic serve was putting the site up as a server, but when using on a mobile device ionic launches the app using the file:// protocol and the leading slash was causing conflicts. 我在ionic伺服器将网站设定为伺服器的网址中,档案的网址中有一个正斜线,但是当在行动装置上使用ionic时,则使用file://通讯协定启动应用程式造成冲突。

tldr; tldr; removed the leading slash, all is well. 删除了斜杠,一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM