how can I retrieve an html document from a url from javascript?

微笑、不失礼 提交于 2019-12-11 23:29:52

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/2234847/how-can-i-retrieve-an-html-document-from-a-url-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!