简体   繁体   English

XmlHttpRequest.responseText结果?

[英]XmlHttpRequest.responseText result?

I am new to JavaScript. 我是JavaScript新手。 I need to test the output of XMLHttpRequest.responseText on a given URL. 我需要在给定的URL上测试XMLHttpRequest.responseText的输出。 What is the simplest way to do it? 最简单的方法是什么?

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. main-content是一个<div>标记。 The last line of code will replace the contents of the <div> tag with output of xmlHttp.responseText . 最后一行代码将<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. 现在,当我在常规浏览器中打开m.google.com并选择“查看源代码”时,源代码的哪一部分将放置在<div>标记内。 Or let's stay I have to test this code in - where do I write this code? 或者让我们留下来,我必须在以下位置测试此代码-我在哪里编写此代码?

Actually, I have an Android app that displays this HTML code result in a WebView . 实际上,我有一个Android应用程序,可以在WebView中显示此HTML代码结果。

Skip your frustrations and employ jQuery . 跳过烦恼并使用jQuery It's an indestry standard and almost every employer asks if you have experience with it. 这是一个行业标准,几乎每个雇主都会询问您是否有经验。

$.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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM