离子错误 404 未找到离子 运行 android

ionic error 404 not found with ionic run android

我有一个 ionic 应用程序,当我向我的本地主机发出 POST orGET 请求时,它与 ionic serve 配合得很好,当我发出 ionic uild android 然后 ionic 运行 android 我总是得到 404 POST 未找到, 我几乎尝试了一切。 我添加了插件白名单并放入我的 config.xml

  <content src="index.html"/>
  <access origin="*"/>
  <allow-intent href="*"/>
  <allow-navigation href="*"/>

在索引 html 中,我尝试了这些元数据(注释了其中一个,取消了注释另一个)

    <!-- <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作为后端,我觉得cors配置的不错:

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"
})

评论 urls 是我尝试过的所有选项:我用 ifconfig 得到它们,当它是我的 phone 我禁用了我电脑上的 wifi 并使用了我的连接 usb phone(所以 ip 在 ifconfig 中是 usb0)。我还在 chrome://inspect/#devices 上使用端口转发在 app.js 中尝试了 url“http://localhost:1337”(通过将 localhost:1337 我的电脑) .对于我的文件 app.js 中列出的 url,我禁用了端口转发

但是所有配置总是报错POST 404 not found

有什么想法吗?

我想我以前遇到过这个问题。

原因是当您 运行 ionic serveionic run android -l 它 运行 代码(www 文件夹)托管在您的计算机上,而不是设备上。所以它可以直接访问localhost。但是当你在真实设备上构建它并 运行 时,那里没有托管本地主机,然后 404 NOT FOUND 发生。

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

  • 获取您计算机的 LAN/Wi-fi IPv4(例如:192.168.1.2
  • http://localhost 中的 url 替换为您的计算机 IP。
  • 在最后添加您的端口。 (例如:http://192.168.1.2:1337
  • 运行 ionic run android.