Javascript XmlHttpRequest.responseText 结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5590125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XmlHttpRequest.responseText result?
提问by Sagar Hatekar
I am new to JavaScript. I need to test the output of XMLHttpRequest.responseText
on a given URL. What is the simplest way to do it?
我是 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
is a <div>
tag. The last line of code will replace the contents of the <div>
tag with output of xmlHttp.responseText
.
main-content
是一个<div>
标签。最后一行代码将<div>
用输出替换标签的内容xmlHttp.responseText
。
Now, when I open m.google.com
in my regular browse and select "View Source", what part of the source gets placed within the <div>
tag. Or let's stay I have to test this code in - where do I write this code?
现在,当我m.google.com
在常规浏览器中打开并选择“查看源代码”时,源代码的哪一部分被放置在<div>
标签中。或者让我们留下来我必须在其中测试此代码 - 我在哪里编写此代码?
Actually, I have an Android app that displays this HTML code result in a WebView
.
实际上,我有一个 Android 应用程序,它以 .html 格式显示此 HTML 代码结果WebView
。
回答by jbcurtin
Skip your frustrations and employ jQuery. It's an indestry standard and almost every employer asks if you have experience with it.
跳过您的挫折并使用jQuery。这是一个行业标准,几乎每个雇主都会问你是否有这方面的经验。
$.get({
url:'url.html',
data:{},
success:function(evt){console.log(evt);}
});
However, if you want a go a more difficulte route:
但是,如果您想走一条更困难的路线:
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)
回答by Daniel Protopopov
Get Firefox with Firebug and browse XMLHttpRequest object for responseText member.
使用 Firebug 获取 Firefox 并浏览 XMLHttpRequest 对象以获取 responseText 成员。