构建AJAX调用的回送URL的最佳方式是什么?假设我有一个网页Loopback jQuery AJAX请求
http://www.mydomain.com/some/subdir/file.php
我想加载一个AJAX调用。在Firefox中,使用jQuery这工作得很好:
$.post('/some/subdir/file.php', ...);
的Safari/WebKit的试图解释为本地文件系统上的位置(所以它最终翻译成“文件://some/subdir/file.php” )。这不仅不指向任何有用的东西,还会导致安全错误。
有没有一种方法可以实现这一点,而无需将域硬编码到URL中?我想尽可能使其与域独立。
更新
我结束了从location.href解析出的基本URL和将其掷入一个可访问的jQuery函数如下:
/**
* Retrieves the current root URL.
*
* @return string the root URL
*/
$.fn.rootUrl = function() {
var url = location.href;
return url.substring(0, url.indexOf('/', 7));
};