通过AJAX请求HTML页面 [英] Request an HTML page via AJAX

查看:108
本文介绍了通过AJAX请求HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




Ajax问题:我是否可以检索HTML页面并使用DOM以类似的方式从请求的页面中获取

内容一个XML页面

以同样的方式请求?


详细信息:我正在通过以下方式请求网页:


myRequest.open(" GET"," http://www.asite.com/page.html" ;, true);


我可以接受回复通过以下方式将其转换为字符串:


myRequest.responseText.toString();


因此我可以解析它以找到一些东西在我正在寻找的b $ b页面上。如果我要检索XML文档,我可以使用DOM

轻松地从页面中获取内容。是否有一个AJAX-esque方法

来检索HTML页面并使用DOM在

页面上查找内容?将html页面转换为字符串并使用

javascript字符串方法解析它是地狱。


谢谢大家,

J

Hi,

Ajax question: Can I retrieve an HTML page and use the DOM to grab
stuff from the requested page in a similar fashion to an XML page
requested in the same manner?

Details: I''m requesting a web page by:

myRequest.open("GET", "http://www.asite.com/page.html", true);

I can take the response and convert it to a string via:

myRequest.responseText.toString();

So that I can parse it to find a few things on the page that I am
looking for. If I were to retrieve an XML document, I can use the DOM
to grab stuff from the page with ease. Is there an AJAX-esque method
to retrieving an HTML page and using DOM to look for things on the
page? Converting an html page to a string and parsing it with
javascript string methods is hell.

Thanks everyone,
J

推荐答案

J写道:
J wrote:

详情:我在请求一个网页:


myRequest.open(" GET"," http://www.asite.com/page.html" ;, true);


我可以通过以下方式获取响应并将其转换为字符串:


myRequest.responseText.toString();
Details: I''m requesting a web page by:

myRequest.open("GET", "http://www.asite.com/page.html", true);

I can take the response and convert it to a string via:

myRequest.responseText.toString();



responseText给你一个字符串,你为什么要调用toString()?

responseText gives you a string, why do you call toString() on that?


所以我可以解析它,在页面上找到一些我想要的东西。如果我要检索XML文档,我可以使用DOM

轻松地从页面中获取内容。是否有一个AJAX-esque方法

来检索HTML页面并使用DOM在

页面上查找内容?
So that I can parse it to find a few things on the page that I am
looking for. If I were to retrieve an XML document, I can use the DOM
to grab stuff from the page with ease. Is there an AJAX-esque method
to retrieving an HTML page and using DOM to look for things on the
page?



如果发送的文档是格式正确的XHTML,那么你可以使用responseXML。

到目前为止,没有浏览器公开它的HTML解析器同样地,如果你想要解析HTML文档,你最好将它们加载到

iframe(可以看不见)。

-


Martin Honnen
http:/ /JavaScript.FAQTs.com/


Martin Honnen写道:
Martin Honnen wrote:

responseText给你一个字符串,你为什么要调用toString()?
responseText gives you a string, why do you call toString() on that?



好​​问题。我按照一些使用它的例子。我将删除

toString()调用。

Good question. I followed some examples that used it. I will drop the
toString() call.


如果发送的文档是格式良好的XHTML,那么你可以使用responseXML。 />
到目前为止,没有浏览器以相同的方式公开其HTML解析器,如果你想要解析HTML文档,你最好将它们加载到
$ b $中。 b iframe(可以是不可见的)。
If the document sent is well-formed XHTML then you can use responseXML.
So far there is no browser exposing its HTML parser the same way, if you
want to parse HTML documents you are better off loading them into an
iframe (that can be invisible).



将页面加载到iframe中。听起来是一个好主意。我会尝试。

我正在查看的页面类型不是格式良好的XHTML。我可以使用responseXML来获取这些页面并使用DOM函数吗?我想b $ b我觉得我试过这个并遇到了一些麻烦。

Load page into an iframe. Sounds like a good idea. I will try that.
The types of pages I am looking at are not well-formed XHTML. Can I
still use responseXML to grab those pages and use DOM functions? I
think I tried this and had some trouble.




J schrieb:

J schrieb:

Martin Honnen写道:
Martin Honnen wrote:

responseText给你一个字符串,你为什么要调用toString()?
responseText gives you a string, why do you call toString() on that?



好​​问题。我按照一些使用它的例子。我将删除

toString()调用。


Good question. I followed some examples that used it. I will drop the
toString() call.


如果发送的文档是格式良好的XHTML,那么你可以使用responseXML。 />
到目前为止,没有浏览器以相同的方式公开其HTML解析器,如果你想要解析HTML文档,你最好将它们加载到
$ b $中。 b iframe(可以是不可见的)。
If the document sent is well-formed XHTML then you can use responseXML.
So far there is no browser exposing its HTML parser the same way, if you
want to parse HTML documents you are better off loading them into an
iframe (that can be invisible).



将页面加载到iframe中。听起来是一个好主意。我会尝试。

我正在查看的页面类型不是格式良好的XHTML。我可以使用responseXML来获取这些页面并使用DOM函数吗?我想b $ b我认为我试过这个并且遇到了一些麻烦。


Load page into an iframe. Sounds like a good idea. I will try that.
The types of pages I am looking at are not well-formed XHTML. Can I
still use responseXML to grab those pages and use DOM functions? I
think I tried this and had some trouble.



如果你的浏览器不支持
支持,只需使用Iframe来模拟XMLHttpRequest。例如。 IE 5/6(不带ActiveX)或Opera 7.否则你应该

当然要使用原生请求。要发出跨域请求,请使用通过HTTP读取外部文件的

php文件。


提供此功能的框架是UniAjax。 /> http://uniajax.net


Andi

Just use Iframes to emulate an XMLHttpRequest, if your browser does not
support it. E.g. IE 5/6 (without ActiveX) or Opera 7. Else you should
of course use a native request. To make a cross-domain request use a
php file that reads an external file via HTTP.

A framework that provides this functionality is UniAjax.
http://uniajax.net

Andi


这篇关于通过AJAX请求HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆