来自服务器的响应
当我向服务器创建请求时:
<script language="javascript" type="text/javascript">
function ajaxFunction()
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
} ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.write(ajaxRequest.responseText);
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "http://www.bbc.co.uk", true);
ajaxRequest.send(null);
}
</script>
为什么响应没有任何内容?为什么响应不是该网站的 html 代码?
When I create request to the server:
<script language="javascript" type="text/javascript">
function ajaxFunction()
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.write(ajaxRequest.responseText);
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "http://www.bbc.co.uk", true);
ajaxRequest.send(null);
}
</script>
Why response is nothing? Why response isnt html code of this web site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以工作(我现在没有时间测试它。)
一些注意事项:
}
后面加上;
+
附录:
跨域ajax请求非常很难正确执行(即安全、稳妥且不会抛出错误)——它们被同域来源政策。
请参阅此问题和这个 有关该主题的更多讨论以及使用代理或使用
解决该问题的方法jsonp
+
jQuery 的 ajax 函数是 325 行长(这还不包括$.ajax.settings 或
$.extend()
)This should work (I don't have time to test it right now.)
A few notes:
}
with a;
+
ADDENDUM:
Cross-domain ajax requests are very difficult to do right (i.e. safely, securely, and without throwing errors) -- they are forbidden to javascript directly by the Same-Domain Origin policy.
See this question and this one for more discussion on the subject and ways to get around it with a proxy or with
jsonp
+
jQuery's ajax function is 325 lines long (and that's not counting$.ajax.settings
or$.extend()
)您可能会考虑使用像 jQuery 这样的库来执行 AJAX 请求 - 它们会为您处理所有跨浏览器的问题,并执行大量额外的工作。你的代码可以很简单
You might consider using a library like jQuery to do AJAX requests - they handle all the cross-browser quirks for you, and do a lot of extra work to boot. Your code could be as simple as