我是 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
中显示此 HTML 代码结果.
最佳答案
跳过你的挫折并使用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)
关于javascript - XmlHttpRequest.responseText 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5590125/