来自服务器的响应

发布于 2024-08-25 07:09:40 字数 735 浏览 3 评论 0原文

当我向服务器创建请求时:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-09-01 07:09:40

应该可以工作(我现在没有时间测试它。)

function ajaxFunction() { //Added open {
    var ajaxRequest;
    try{
    ajaxRequest = new XMLHttpRequest();
    } catch (e){
// Removed additional try / catch
        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;
        }
    }; // Added semi-colon to the end of the anonymous function definition
ajaxRequest.open("GET", "http://www.bbc.co.uk", true);
ajaxRequest.send(null); 
}

一些注意事项:

  • Javascript中的大部分不需要空格,但是适当的缩进使它更多< /strong> 更容易发现语法错误。
  • 当您将属性绑定到匿名函数时,您需要在 } 后面加上 ;
  • 一旦您了解了其工作原理,请深入研究较大的 ajax 函数/模块库之一。 (学习总是一件好事,ajax 是真正需要几十个工时才能遇到浏览器之间所有差异的领域之一。)+

附录:

跨域ajax请求非常很难正确执行(即安全、稳妥且不会抛出错误)——它们被同域来源政策

请参阅此问题这个 有关该主题的更多讨论以及使用代理或使用 解决该问题的方法jsonp

+ jQuery 的 ajax 函数是 325 行长(这还不包括 $.ajax.settings 或 $.extend())

This should work (I don't have time to test it right now.)

function ajaxFunction() { //Added open {
    var ajaxRequest;
    try{
    ajaxRequest = new XMLHttpRequest();
    } catch (e){
// Removed additional try / catch
        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;
        }
    }; // Added semi-colon to the end of the anonymous function definition
ajaxRequest.open("GET", "http://www.bbc.co.uk", true);
ajaxRequest.send(null); 
}

A few notes:

  • White space is not required for the most part in Javascript, but proper indentation makes it much easier to spot syntax errors.
  • When you bind an attribute to an anonymous function you need to follow your } with a ;
  • Once you understand how this works, dig into one of the larger libraries ajax functions / modules. (Learning is always a good thing, and ajax is one of those areas that really needs a few dozen man-hours of work to encounter all the differences between browsers.)+

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())

单调的奢华 2024-09-01 07:09:40

您可能会考虑使用像 jQuery 这样的库来执行 AJAX 请求 - 它们会为您处理所有跨浏览器的问题,并执行大量额外的工作。你的代码可以很简单

$.get(
  'http://www.bbc.co.uk',
  function(data) {
    // do something with data
  }
);`

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

$.get(
  'http://www.bbc.co.uk',
  function(data) {
    // do something with data
  }
);`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文