weixin_33708432 2014-04-13 17:24 采纳率: 0%
浏览 24

Ajax XmlHttpRequest

I have js code as well as *.txt file with some text that I want to load to the page.

JS Code:

 (function () {
 var link = document.getElementsByTagName("a")[0];
 link.onclick = function () {
      var xhr = new XmlHttpRequest();
      xhr.onreadystatechange = function () {
         if ((xhr.readyState == 4) && (xhr.status == 200 || xhr.status == 304)) {
                xhr.responseText;
                var body = document.getElementsByTagName("body")[0];
                var p = document.createElement("p");
                var pText = document.createTextNode(xhr.responseText);
                p.appendChild(pText);
                body.appendChild(p);
            }
        };
        xhr.open("Get", "ajax.txt", true);
        hxr.send(null);
        return false;
    };
})();

HTML Code:

<body>
<h1>AjaxTest</h1>
<a href="ajax.txt">Load the text from file</a>
<script src="main.js">
</script>

Everything should work. However ReSharper underlines XmlHttpRequest(); and says Use of an implicitly declared global variable" and for this xhr.responseText; it says - Expression statement is not assignment of call. What is the problem?

  • 写回答

1条回答 默认 最新

  • weixin_33724659 2014-04-13 17:46
    关注

    A few comments:

    • Capitalize "XML":

      var xhr = new XmlHttpRequest(); -> var xhr = new XMLHttpRequest();


    • A variable is not a statement:

      xhr.responseText;, just get rid of this line, it's like saying var a = 5; and then a;


    • You can use document.body to get the body element:

      var body = document.getElementsByTagName("body")[0]; ->

      var body = document.body;


    • You have no variable named hxr:

      hxr.send(null); -> xhr.send(null);


    If you're following this is what you should get:

     (function () {
     var link = document.getElementsByTagName("a")[0];
     link.onclick = function () {
          var xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function() {
             if ((xhr.readyState == 4) && (xhr.status == 200 || xhr.status == 304)) {
                    var body = document.body;
                    var p = document.createElement("p");
                    var pText = document.createTextNode(xhr.responseText);
                    p.appendChild(pText);
                    body.appendChild(p);
                }
            };
            xhr.open("Get", "ajax.txt", true);
            xhr.send(null);
            return false;
        };
    })();
    

    If I were you I'd prefer using jQuery:

    $('a').first().click(function() {
        $.get('ajax.txt', function(data) {
            $(document.body).append('<p>' + data + '</p>');
        });
    });
    

    This is your entire code using jquery ;)

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统