Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 使用base href时请求url问题_Apache_Angular_Angular Cli - Fatal编程技术网

Apache 使用base href时请求url问题

Apache 使用base href时请求url问题,apache,angular,angular-cli,Apache,Angular,Angular Cli,快速版-我相信我想要的: 如何将angular2 http服务配置为忽略 更长的版本-如果有更好的方法: 在我的项目中,我们使用Apache为两个angular应用程序提供服务: http://host/index.html - angular 1 http://host/ng2/index.html - angular 2 它们都使用相同的端点 http://host/api/foo 两个应用程序使用相同的vhost。angular 2应用程序位于磁盘上的public/ng2下 问题归结为

快速版-我相信我想要的: 如何将
angular2 http
服务配置为忽略

更长的版本-如果有更好的方法: 在我的项目中,我们使用Apache为两个angular应用程序提供服务:

http://host/index.html - angular 1
http://host/ng2/index.html - angular 2
它们都使用相同的端点

http://host/api/foo
两个应用程序使用相同的vhost。angular 2应用程序位于磁盘上的public/ng2下

问题归结为:

当我使用时:
angular应用程序加载良好。 但是,使用http服务对api的所有请求都会受到污染

http.get('/api/foo/2') goes to http://host/ng2/api/foo
我目前的解决方案是将base href设置为
,并将除index.html之外的所有angular2文件直接放在/public下。(index.html仍在/public/ng2中)这是可行的,但它是一团混乱

我想要什么:

  • 所有angular2文件都在/public/ng2中(由于
    或其他Apache配置而起作用)
  • 传出请求没有附加ng2。
    • e、 g http.get('/api/foo/2')=>

这不是硬编码url的选项

最后我按照Ki Jey的建议做了,并使用了一个绝对url来访问API

service.base.ts: 
protected getAbsoluteUrl(relativeUrl : String) : String { 
  return document.location.protocol + "//" + document.location.host + "/" + relativeUrl
}

service.ts:
class service extends BaseService
  getTheFoo(){ 
    http.get(this.getAbsoluteUrl(foo/2))...
  }

只需将
'/'
附加到api调用将停止附加
baseHref

getFoo():可观察{
常量url='/'+'api/foo';
返回此.http.get(url);
}

我建议为HTTP调用使用带有API完整基本url的$config变量,并像这样使用HTTP:
HTTP.get(API_root+'/foo/2')
谢谢@KiJéy。我忘记提到的是,我们部署了多个实例。每个都有自己的/api/foo/2。例如(和)。所以相对路径会更好。你能解释一下为什么你认为相对路径会更好吗?我同意@KiJéy,将URL放入环境文件并使用绝对路径。我上面描述的问题肯定可以使用绝对路径解决。当我写这个问题时,我从未认真考虑过以编程方式创建绝对路径的选项。例如fullUrl=document.location.protocol+“/”+document.location.host+“/”+relativeUrl。这种方法确实有效——谢谢!