简体   繁体   English

从其他域加载网页

[英]Load webpage from different domain

I am creating a sample project in MVC. 我正在MVC中创建一个示例项目。 Suppose I have two projects A and B. In project AI have a javascript file which takes index page of project A and put it in a div having id Widget . 假设我有两个项目A和B。在项目AI中,有一个javascript文件,该文件将project A索引页放入具有id Widget的div中。

In project B index page I have a reference to the javascript file of project A and a div with id Widget . project B索引页中,我引用了项目A的javascript文件和ID为Widget的div。

On page load I want to load Project A's index page to Project B's Widget Div . 在页面加载时,我想将Project A's index page加载到Project B's Widget Div

Here is my code 这是我的代码

Project A

Index.cshtml (Index view)

@{
ViewBag.Title = "Index";
}  
<h2>Index</h2>
<div id="data">Thi is index page.</div>

Widget.js (JavaScript file)

$(document).ready(function (e) {
$('#widget').load('/Home/Contact', function (response, status, xhr) {
    if (status == "error") {
        var msg = "Sorry but there was an error: ";
        alert(msg + xhr.status + " " + xhr.statusText);
    }
});
};

Here is another project 这是另一个项目

Project B

Index view which call the project A's index view
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="http://localhost:2455/Scripts/Widget.js"></script>

<div id="widget"></div>

<div>Project B Index</div>

Above sample code display index view of Project B while I want to display index view of Project A 上面的示例代码显示项目B的索引视图,而我想显示项目A的索引视图

Without full url it will not work the way you want. 没有完整的URL,它将无法按您想要的方式工作。 To enable cross domain request you need to add 2 headers to your web.configs <system.webServer> <httpProtocol> <customHeaders> <add name="access-control-allow-headers" value="content-type"/> <add name="Access-Control-Allow-Origin" value="*"/> </customHeaders> </httpProtocol> <system.webServer> Or the other way is in your Contact controller of project B to make http request to controller of project A, then you will get HTML of controller of project A without CORS issue 要启用跨域请求,您需要向web.configs中添加2个标头<system.webServer> <httpProtocol> <customHeaders> <add name="access-control-allow-headers" value="content-type"/> <add name="Access-Control-Allow-Origin" value="*"/> </customHeaders> </httpProtocol> <system.webServer>或另一种方式是在项目B的Contact控制器中向控制器发出http请求项目A的代码,那么您将获得项目A的控制器的HTML,而没有CORS问题

The second solution is 第二个解决方案是

  1. Make request to the local controller 向本地控制器发出请求

  2. In the local controller call to the code bellow, this code makes request to some url and returns all that retrieved from the url. 在下面的代码的本地控制器调用中,此代码向某个URL发出请求,并返回从URL中检索到的所有内容。

    var html = ""; StreamReader reader = null; WebResponse response = null; try { var request = WebRequest.Create("url of project A controller"); response = request.GetResponse(); var dataStream = response.GetResponseStream(); reader = new StreamReader(dataStream); //here you will get HTML of controller from project A html = reader.ReadToEnd(); } finally { if (reader != null) { reader.Close(); } if (response != null) { response.Close(); } } ViewBag.HTML = html;//And then you can use the HTML inside of current controller, simply put @ViewBag.HTML inside of the div.

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

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