JQuery将页面内容加载到Div中

时间:2011-07-12 20:01:01

标签: jquery html load

我发现很多关于如何将页面内容加载到div中的问题/答案;但是,我不能让我加载。我在document.ready中运行jquery。单独运行警报工作正常,但下面的代码没有。任何指针都将非常感激。谢谢!

//First I tried this:
$("#divTestPreview").load("~/Testing/TestCreation/AddTestItems.aspx");
//and then I tried this:
$.get('~/Testing/TestCreation/AddTestItems.aspx', function (data) {
            $('#divTestPreview').html(data);
            alert('Load was performed.');
});

<div id="divTestPreview" runat="server" style="width: 100%; height: 500px;">
</div>

4 个答案:

答案 0 :(得分:2)

尝试删除div中的runat =“server”,因为我认为你的ASP.NET解释器在到达客户端之前可能会弄乱它。

谢谢!

答案 1 :(得分:1)

你可以使用load并且只能加载你域内的页面,否则你会得到跨域错误有一个解决方案,你需要创建一些代理读取这个:WebBrowser Control: Disable Cross Site XSS Filtering or another way to process JS in full on HTML或者你可以尝试这个:http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

答案 2 :(得分:1)

你的div有runat =“server”标签,这使它成为服务器端控件。要在客户端访问它,您需要使用ClientID来访问div。 喜欢这个

$("#<% divTestPreview.ClientID %>").load();

答案 3 :(得分:0)

您正在制作的Access-Control-Allow-Origin不允许这样做 跨域AJAX,在当前版本的浏览器中是不可能的, 你必须在服务器端这样做,例如在PHP中 fopen()o file_get_contents(),如果你需要在客户端进行, 现代浏览器有很多选项,如flensed FLXHR或CORS。