[英]ionic error 404 not found with ionic run android
我有一个离子应用程序,当我向localhost发出POST或GET请求时,可以与离子服务一起很好地工作;当我使离子uild android成为bt时,我又收到bt,我总是得到一个未找到的404 POST,我尝试了几乎所有方法。 我已经添加了插件白名单,并放入了config.xml
<content src="index.html"/>
<access origin="*"/>
<allow-intent href="*"/>
<allow-navigation href="*"/>
在索引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作为后端,我认为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来获取它们,在手机情况下,我禁用了计算机上的wifi并使用了手机的usb连接(因此,ipconfig中的IP为usb0)。 我还使用chrome:// inspect /#devices上的端口转发在app.js中尝试了URL“ http:// localhost:1337 ”(通过将设备的localhost:1337分配给设备的端口1337)。 对于文件app.js中列出的网址,我禁用了端口转发
但是所有配置始终会给出错误POST 404 not found
有任何想法吗?
我想我以前遇到过这个问题。
原因是当您运行ionic serve
或ionic run android -l
它将运行从计算机而非设备托管的代码(www文件夹)。 因此它可以直接访问localhost
。 但是,当您构建它并在真实设备上运行时,那里没有托管本地主机,则发生404 NOT FOUND
。
这就是我解决问题的方式(仅适用于相同的Wi-fi网络)。
192.168.1.2
) http://192.168.1.2:1337
: http://192.168.1.2:1337
: http://192.168.1.2:1337
) ionic run android
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.