如何在获取站点的原点时包含任何子目录?

时间:2018-03-08 08:36:54

标签: javascript angular

我正在尝试使用以下方法获取项目运行的URL:

const url = window.location.origin;

它有效,但我面临的问题是任何子目录都没有被选中。因此,当我为https://my-app.com/ui运行时,只会选择https://my-app.com

https://my-app.com指向https://my-app.com/ui,这也是我的应用运行的位置。

我能想到的唯一解决方案是拆分window.location.pathname但当原点未使用任何子目录时会中断,例如http:localhost:4200https://dev.myApp.com

如何修改上面的代码以包含子目录,以便它适用于所有上述场景?

修改

我在Angular 5上,所以也许有一种Angular方式?

给定网址的预期输出:

https://myApp.com/ui/dashboard/events -> https://myApp.com/ui 
http://localhost:4200 -> http://localhost:4200 
https://sites.dev.myApp.com/ui2/dashboard/events -> https://sites.dev.myApp.com/ui2 
https://sites.dev.myApp.com/ui2/app/dashboard/events -> shttps://ites.dev.myApp.com/ui2/app/dashboard/events

1 个答案:

答案 0 :(得分:0)

只需从origin

中取/ pathname之前的var output = location.origin + ( location.pathname ? location.pathname.split( "/" )[0] : "" ) 和第一个值
import requests
import execjs

url = 'http://www.sgx.com/JsonRead/JsonstData?qryId=RAll'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
page = requests.get(url,headers=headers)

content = page.content[len('{}&& '):] if page.content.startswith('{}&& ') else page.content
data = execjs.get().eval(content)
print(data)