实现功能:
实际项目中需要在A站点下的html页面输出B站点下的新闻数据,两个项目站点不在相同域下,因为A站没有数据操作权限,无法直接获取,只能通过B站点进行数据返回。
技术难点:
用 js get/post 请求,会有跨域问题
A站:
新建一个 news.aspx 页面,页面只保留下面这一段,其余段落删除
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="news.aspx.cs" Inherits="Output.news" %>
news.aspx 代码
protected void Page_Load(object sender, EventArgs e)
{
/*
输出到html的样式
<div id="news">
<ul>
<li><a href="http://localhost/1.html" target="_blank">标题</a></li>
<li><a href="http://localhost/2.html" target="_blank">标题</a></li>
<li><a href="http://localhost/3.html" target="_blank">标题</a></li>
<li><a href="http://localhost/4.html" target="_blank">标题</a></li>
<li><a href="http://localhost/5.html" target="_blank">标题</a></li>
</ul>
</div>
*/
string content = "";
content += "<div id=\"news\"><ul>";
for (int i = 0; i < 10; ++i)
{
content += "<li><a href=\"http://localhost/1" + i + ".html\" target=\"_blank\">标题1" + i + "</a></li>";
}
content += "</ul></div>";
Response.Write("document.write('"+ content + "')");
Response.ContentType = "text/html";
Response.End();
}
B站:
新建 index.html 页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻列表</title>
</head>
<body>
<script src="http://localhost:8102/news.aspx" type="text/javascript"></script>
</body>
</html>
A站网址:http://localhost:8102
B站网址:http://localhost:8103
在浏览器中打开B站测试页