2

我是 JavaScript 新手。我需要测试XMLHttpRequest.responseText给定 URL 的输出。最简单的方法是什么?

var url = "http://m.google.com/"; <br>
var xmlHttp = new XMLHttpRequest(); <br>
xmlHttp.open('GET', url, true); <br>
document.getElementById('main-content').innerHTML = xmlHttp.responseText; <br>

main-content是一个<div>标签。最后一行代码会将<div>标签的内容替换为xmlHttp.responseText.

现在,当我m.google.com在常规浏览中打开并选择“查看源代码”时,源代码的哪一部分被放置在<div>标签中。或者让我们留下来测试这段代码——我在哪里写这段代码?

实际上,我有一个 Android 应用程序,它以WebView.

4

2 回答 2

4

跳过你的挫折并使用jQuery。这是一个行业标准,几乎每个雇主都会问你是否有这方面的经验。

$.get({
 url:'url.html',
 data:{},
 success:function(evt){console.log(evt);}
});

但是,如果您想走更困难的路线:

var url = "http://m.google.com/"; 
var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", url,true);

 // subscribe to this event before you send your request.
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   //alert the user that a response now exists in the responseTest property.
   alert(xmlhttp.responseText);
   // And to view in firebug
   console.log('xhr',xmlhttp)
  }
 }
 xmlhttp.send(null)
于 2011-04-08T04:16:17.763 回答
0

使用 Firebug 获取 Firefox 并浏览 XMLHttpRequest 对象以获取 responseText 成员。

于 2011-04-08T04:06:20.203 回答