jQuery使用变量组合URL
jQuery combining URL using a variable
上传至托管服务器后,以下代码成功转为http://myexample.com/otherpartsofpath
:
ajax: {
url: 'http://' + window.location.hostname + '/otherpartsofpath',
type: 'GET',
...
但是当在本地计算机上渲染时,它会给出一个错误。这就是为什么在这个例子中,我应该显式定义URL:
ajax: {
url: 'http://localhost:10930/otherpartsofpath',
type: 'GET',
...
如何使代码在没有明确定义URL的情况下在本地工作?
使用相对路径/test
将始终产生正确的协议、主机名和端口。因此,为了方便起见,您总是可以使用:
ajax: {
url: '/otherpartsofpath',
type: 'GET',
...
在http://test.com/directory/test.html
上运行时为http://test.com/otherpartsofpath
.
在http://localhost:1234/directory/test.html
上运行时,它是http://localhost:1234/otherpartsofpath
.
出于安全考虑,你不能。
使用本地服务器进行测试。如果要从远程服务器获取数据,请设置代理
如果你不在本地80端口上你就必须这样做
var hostName = window.location.hostname;
if ( window.location.port != '') {
hostName += ':' + window.location.port;
}
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- 使用php或javascript从facebook相册URL中删除多余的部分
- Angular JS IE9 Hashbang url rewriting
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 直接下载文件,而不是从window.open(url)
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 通过js在新选项卡中有条件地打开url
- 根据id将json数组组合为一个json数组
- CKFinder 3为所选文件返回错误的URL
- 类型错误:url未定义extjs 4正在填充组合框
- 如何正确地缩小/组合网页项目中的CSS/JS与url重写
- 如何制作具有不同URL的组合搜索框
- Html中Url的路径组合
- 在Backbone.js中组合哈希和非哈希URL
- 需要从多选选项组合值形成一个url与javascript
- jQuery使用变量组合URL
- 从value中提取字符串,并用这个字符串组合URL
- 防止标准的a href/jquery单击组合将#附加到url
- React Native Text Components:如何组合 url 和其他字符串
- 组合bookmarklet来创建两个URL之间的切换