【问题标题】:XMLHttpRequest is not defined in Cloud9 IDE?Cloud9 IDE 中没有定义 XMLHttpRequest?
【发布时间】:2012-03-08 22:20:20
【问题描述】:

我在 Cloud9 IDE 中定义了一个 XMLHttpRequest,如下所示:

var xhr = new XMLHttpRequest();

但它提示错误,说ReferenceError: XMLHttpRequest is not defined

XMLHttpRequest 是否在 Cloud9 IDE 中定义?

谢谢!

【问题讨论】:

    标签: xmlhttprequest cloud9-ide


    【解决方案1】:

    如果您通过“调试”按钮运行您的 javascript 文件,那么它将在不包含 XMLHttpRequestnode.js 中执行(因为它是浏览器功能)。

    如果您正在开发的文件是客户端文件,请通过“预览”按钮打开引用 js 文件的 HTML 页面。

    否则,您可以使用模仿浏览器行为的node-XMLHttpRequest,或request,这是向其他服务请求nodejs 的分解标准。

    【讨论】:

      【解决方案2】:

      我知道这是不久前被问到的,但如果其他人遇到同样的问题,这里是如何解决它。

      您需要先在函数中定义 XMLHttpRequest,然后再尝试使用它创建对象。

      这是一个例子:

      //Define your method reference 
      function createXMLHttpRequest() 
         {
          try 
          { 
            return new XMLHttpRequest();
             } catch(e) {}
          try 
          { 
         return new ActiveXObject("Msxml2.XMLHTTP"); 
         } catch (e) {}
          alert("Sorry, the XMLHttpRequest is not supported");
          return null;
          }
      

      //现在可以开始使用定义的createXMLHttpRequest()创建对象了

      var xhr = new XMLHttpRequest();
      
      function oHttp_readyStateChange() 
      {
      if (oHttp.readyState == 4) 
      {
      if (oHttp.status == 200) 
      {
        alert(oHttp.responseText); 
      }
      else {
        alert("The server returned a status code of " + oHttp.status);
      }
      }
      }
      oHttp.open("GET", "http://myWebsite/Myfile.sql", true);
      
      oHttp.onreadystatechange = oHttp_readyStateChange;
      
      oHttp.send(null);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-20
        相关资源
        最近更新 更多