how can I retrieve an html document from a url fro

2019-08-27 15:14发布

I have a url and I want to retrieve the html dom generated when going to the url (all the code for the page) in a javascript variable.

How can I do this? I'm guessing an html get or post? Can anyone give an example with jQuery?

Every time I do a $.get or $.post like this:

$.get("http://www.google.ca", function(result) { alert(result); alert($(result).html()); });

$.post("http://www.google.ca", function(result) { alert(result); alert($(result).html()); }, "xml");

the first alert in each call comes up blank and the second comes up as null. Any ideas?

Thanks,
Matt

2条回答
Viruses.
2楼-- · 2019-08-27 15:33

You can only fetch documents (in this way) from your own domain due to the same origin policy.

查看更多
Rolldiameter
3楼-- · 2019-08-27 15:51

$("#divId").load("documentname.html");

查看更多
登录 后发表回答