为什么这在 IE 上不起作用?
这是一个示例:
http://develop.davzy.com/ajaxtest/#!/contents< /a>
它适用于除 Internet Explorer 之外的所有浏览器。代码如下:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script>
$(document).ready(function(){
$('div').load('contents.html');
});
</script>
</head>
<body>
<div>This message will change if the call is made correctly.</div>
</body>
</html>
contents.html 的内容是 如果您仍在 test.html 上,则此 AJAX 调用有效。
显然,我计划使用主题标签导航,但奇怪的是,如果您从 url 中删除 /#!/contents ,它将在 IE 中工作。但如果你把它留在那里,即使它与代码无关,它仍然不起作用。查看标题,我收到 406 错误。帮助 :(
Here is an example:
http://develop.davzy.com/ajaxtest/#!/contents
It works on every browser except for internet explorer. Here's the code:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script>
$(document).ready(function(){
$('div').load('contents.html');
});
</script>
</head>
<body>
<div>This message will change if the call is made correctly.</div>
</body>
</html>
The contents of contents.html are
If you're still on test.html, then this AJAX call worked.
Obviously I plan on using hashtag navigation but what's weird is if you remove the /#!/contents from the url it WILL work in IE. But if you leave it there even though it has NOTHING to do with the code it still doesn't work. Looking at the headers I'm getting 406 errors. Help :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我上面的评论:问题似乎是 IE9 在创建“Referer”标头时保留了 URL 的“#!/contents”部分。 jQuery AJAX 围绕浏览器的 XMLHTTPRequest 对象包装的 jqXHR 对象公开了一个 setRequestHeader 方法,该方法可能允许您更改 Referer 标头,这样就不会导致 406 错误。如果您不关心准确的 Referer 标头,您可以尝试将以下代码添加到脚本中:
这将为针对 div 执行的所有 AJAX 操作创建一个 ajaxSend 回调,这些操作将从 Referer 标头中去除哈希标记...我还没有时间测试这个,但理论上它应该有效。
Per my comment above: the issue seems to be that IE9 preserves the "#!/contents" part of the URL when creating the "Referer" header. The jqXHR object that jQuery AJAX wraps around the browser's XMLHTTPRequest object exposes a setRequestHeader method that might let you change the Referer header so that it doesn't cause the 406 error. You might try adding the following code to your script, if you don't care about an accurate Referer header:
This will create an ajaxSend callback for all AJAX operations executed against divs that will strip hash marks out of the Referer header... I haven't had time to test this, but in theory it should work.