rad*_*101 5 html ajax anchor hash jquery
主要问题:如何通过 jQuery.ajax()检索带有“ http://www.example.com/index.html#foo ”之类的 url 的内容?
注意:以下适用于脚本,这是我最初要求的:
var script = document.createElement('script');
script.type= 'text/javascript';
script.src= 'http://www.example.com/script.js#foo';
$('head').append(script);
Run Code Online (Sandbox Code Playgroud)
您可以使用此处找到的方法以普通的旧 ajax 方式构建您自己的请求:
var xmlHttp = new XMLHttpRequest();
var url="http://www.example.com/index.html#foo";
xmlHttp.open("GET", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", 0);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
}
}
xmlHttp.send();
Run Code Online (Sandbox Code Playgroud)
然而,这似乎是 jQuery.ajax 应该支持的东西。
至于jQuery,我有以下几点:
$.ajax({
url: 'http://www.example.com/index.html#foo',
cache: true
});
Run Code Online (Sandbox Code Playgroud)
但是,实际发出的请求是“ http://www.example.com/index.html ”。如果我无法控制我正在使用的端点,并且如果“#foo”不在 url 中,它会以完全不同的内容响应,这很糟糕。
基于对 SO 上另一个问题的回答,我尝试了以下操作
$.ajax({
url: 'http://www.example.com/index.html',
cache: true,
data: {
'': '#foo'
}
});
Run Code Online (Sandbox Code Playgroud)
但是,请求的 url 是“ http://www.example.com/index.html?=#foo ”,它添加了等号和 ? 对于查询字符串(我不想要的东西)。
另一个问题表明您可以对 url 进行编码,然后在 beforeSend 函数中对其进行解码,但我不确定如何修改该位置的请求 url。
由于我已经有了一个工作,这个问题是学术性的,而不是一个非常重要的时间敏感的世界末日问题,但我仍然很好奇。我敢打赌我错过了一些愚蠢的东西。
不幸的是,我在写这篇文章时丢失了我的其他 SO 问题参考。所以对此深表歉意。
在此先感谢您的帮助!
| 归档时间: |
|
| 查看次数: |
330 次 |
| 最近记录: |