Ionic运行Android时出现404未找到错误。

3
我有一个ionic应用,当我向本地主机发出POST或GET请求时,它可以在ionic serve下正常工作,但是当我进行ionic build android然后ionic run android时,我总是得到一个404 POST未找到的错误。我尝试了几乎所有方法。我已经添加了白名单插件并放入了我的config.xml文件中。
  <content src="index.html"/>
  <access origin="*"/>
  <allow-intent href="*"/>
  <allow-navigation href="*"/>

在index.html中,我尝试了这些meta标签(取消注释其中一个并注释掉另一个)。
    <!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self'> -->

    <meta http-equiv="Content-Security-Policy" content="default-src 'self' * ws://localhost:35729 data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *;script-src 'self' localhost:35729 'unsafe-eval' 'unsafe-inline';">

我使用Sails作为后端,我认为跨域资源共享已经很好地配置了:

module.exports.cors = {
/***************************************************************************
 *                                                                          *
 * Allow CORS on all routes by default? If not, you must enable CORS on a   *
 * per-route basis by either adding a "cors" configuration object to the    *
 * route config, or setting "cors:true" in the route config to use the      *
 * default settings below.                                                  *
 *                                                                          *
 ***************************************************************************/
allRoutes: true,

/***************************************************************************
 *                                                                          *
 * Which domains which are allowed CORS access? This can be a               *
 * comma-delimited list of hosts (beginning with http:// or https://) or    *
 * "*" to allow all domains CORS access.                                    *
 *                                                                          *
 ***************************************************************************/
origin: '*',

/***************************************************************************
 *                                                                          *
 * Allow cookies to be shared for CORS requests?                            *
 *                                                                          *
 ***************************************************************************/
credentials: true,

/***************************************************************************
 *                                                                          *
 * Which methods should be allowed for CORS requests? This is only used in  *
 * response to preflight requests (see article linked above for more info)  *
 *                                                                          *
 ***************************************************************************/
methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',

/***************************************************************************
 *                                                                          *
 * Which headers should be allowed for CORS requests? This is only used in  *
 * response to preflight requests.                                          *
 *                                                                          *
 ***************************************************************************/
headers: 'content-type, access-control-allow-origin, authorization'

在app.js的开头,我有以下配置:

};

  angular
.module('frontend', [
  'ionic', 'ionic.service.core',
  'frontend.core',
  'ionic.service.analytics',
  //'cacheapp',
  //'cachemodule',
  'ionic-cache-src',
  'formlyIonic',
  'pascalprecht.translate',
  'angularMoment',
  'ionic.components',
  'ngFacebook',
  'translate.app',
  'translate.form',
  'angular-cache',
  'ngCordova',
  'gettext',
  'module.user',
  'module.gallery'
])
.constant('AccessLevels',{
      anon: 0,
      user: 1,
      admin: 2
})
.constant('BackendConfig',{
  url: "http://api-test-dev.com:1337",
  //url: "http://ip_of_my_phone:1337"
  //url: "10.0.2.2"
  //url: "10.0.2.2:1337"
  //url:"http://ip_of_my_phone"
  //url:"http://ip_of_my_wifi"
  //url:"http://ip_of_my_wifi:1337"
})

注释中的URL都是我尝试过的选项:我使用ifconfig获取了它们,当我的手机需要时,我在电脑上禁用了WiFi并使用了手机的连接USB(因此在ifconfig中IP是usb0)。我还尝试了在app.js中使用端口转发在chrome://inspect/#devices上列出的URL "http://localhost:1337"(通过将设备的端口1337分配给计算机的localhost:1337)。对于在我的文件app.js中列出的URL,我禁用了端口转发。但所有配置都会出现POST 404未找到错误。有什么想法吗?
1个回答

2

我觉得我之前遇到过这个问题。

原因是当你运行 ionic serveionic run android -l 时,它运行的是从你的计算机托管的代码(www 文件夹),而不是设备中的代码。因此,它可以直接访问 localhost。但是当你构建并在实际设备上运行时,其中没有托管 localhost,然后会出现 404 NOT FOUND

这是我解决它的方法(仅适用于相同的 Wi-Fi 网络)。

  • 获取你计算机的局域网 / Wi-Fi IPv4 地址(例如:192.168.1.2
  • 使用你的计算机 IP 替换 http://localhost 的 URL。
  • 在末尾添加你的端口。(例如:http://192.168.1.2:1337
  • 运行 ionic run android

你尝试过在设备内部打开Chrome/浏览器并访问本地主机链接吗? - undefined
刚在设备上进行了测试。如果我使用ifconfig命令访问给定的URL,我会看到Apache页面上打印着 "it orks"。如果我访问htttp://ip_given_for_the_wifi:1337,当Sails正在运行时我会得到一个空白页面(这是正常的,因为服务器上没有显示任何内容)。其他IP地址或者相同IP但不同主机会出现err_connection_refused错误。所以看起来是可以工作的,但是在应用程序中我仍然无法进行REST操作。 - undefined
2/AndroidManifest.xml配置良好,我将尝试3/。 - undefined
我被安装的插件中的白名单插件误导了,所以我以为它已经安装好了。但是,看起来这是由于之前的 Cordova 版本 -> 谢谢 nam - undefined
不客气 @moubert,当你的问题解决时我很高兴。 - undefined
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接