【发布时间】:2011-12-07 20:31:29
【问题描述】:
我正在尝试在我的项目中实现 jQuery 地址插件。
我正在关注 Crawling 实现的 Asual 示例(即 hashbangs)。
我的javascript:
<script type="text/javascript">
$.address.init(function(event) {
// Initializes plugin support for links
$('a:not([href^=http])').address();
var handler = function(data) {
$('.content').html($('#content', data).html()).parent().show();
$.address.title(/>([^<]*)<\/title/.exec(data)[1]);
};
// Loads the page content and inserts it into the content area
$.ajax({
url: '/index.php?ACT=87&action=shows&_escaped_fragment_=' + encodeURIComponent(event.value),
error: function(XMLHttpRequest, textStatus, errorThrown) {
handler(XMLHttpRequest.responseText);
},
success: function(data, textStatus, XMLHttpRequest) {
handler(data);
},
contentType: 'text/html'
});
});
</script>
$.ajax() 调用正在请求我创建的虚拟 html 页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>
<head><title>testing</title>
</head>
<body>
<div id="content">test</div>
</body>
</html>
我正在发送此 HTML 页面,内容类型为 text/html。
请求执行成功,处理程序匿名函数正在获取整个页面数据,但$('.content).html() 命令不起作用。当我执行alert($('#content', data).html()); 时,我得到null,但没有任何反应。没有错误,但也没有内容。
此时我已经不知所措了……有什么建议吗?
编辑:澄清一下,问题不在于请求本身,也不在于 URL,也不是浏览器安全问题。我根本无法从页面上的请求中选择和显示数据。
更糟糕的是,如果我只是将 $.ajax() 网址替换为我知道不存在的页面(即 404 页面)的网址,它会很好地解析并显示我的 404 消息。
【问题讨论】:
标签: jquery ajax deep-linking jquery-address