找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1863|回复: 12

ASP.NET js 输出 aspx 页面内容, 解决 js 跨域请求

 火.. [复制链接]
  • 打卡等级:即来则安
  • 打卡总天数:29
  • 打卡月天数:1
  • 打卡总奖励:7791
  • 最近打卡:2025-12-13 17:25:16

2540

主题

1355

回帖

2万

积分

管理员

积分
21304
发表于 2021-10-24 12:22:08 | 显示全部楼层 |阅读模式
实现功能:
实际项目中需要在A站点下的html页面输出B站点下的新闻数据,两个项目站点不在相同域下,因为A站没有数据操作权限,无法直接获取,只能通过B站点进行数据返回。

技术难点:
用 js get/post 请求,会有跨域问题

A站:
新建一个 news.aspx 页面,页面只保留下面这一段,其余段落删除
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="news.aspx.cs" Inherits="Output.news" %>
复制代码

news.aspx 代码
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     /*
  4.         输出到html的样式
  5.         <div id="news">
  6.             <ul>
  7.                 <li><a href="http://localhost/1.html" target="_blank">标题</a></li>
  8.                 <li><a href="http://localhost/2.html" target="_blank">标题</a></li>
  9.                 <li><a href="http://localhost/3.html" target="_blank">标题</a></li>
  10.                 <li><a href="http://localhost/4.html" target="_blank">标题</a></li>
  11.                 <li><a href="http://localhost/5.html" target="_blank">标题</a></li>
  12.             </ul>
  13.         </div>
  14.      */

  15.     string content = "";
  16.     content += "<div id="news"><ul>";
  17.     for (int i = 0; i < 10; ++i)
  18.     {
  19.         content += "<li><a href="http://localhost/1" + i + ".html" target="_blank">标题1" + i + "</a></li>";
  20.     }
  21.     content += "</ul></div>";

  22.     Response.Write("document.write('"+ content + "')");
  23.     Response.ContentType = "text/html";
  24.     Response.End();
  25. }
复制代码
B站:
新建 index.html 页面
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.     <title>新闻列表</title>
  6. </head>
  7. <body>
  8.     <script src="http://localhost:8102/news.aspx" type="text/javascript"></script>
  9. </body>
  10. </html>
复制代码
实际访问测试:

A站网址:http://localhost:8102
B站网址:http://localhost:8103

在浏览器中打开B站测试页

来源:C#社区
网址:www.hicsharp.com


工控课堂 www.gkket.com

0

主题

111

回帖

261

积分

注册会员

积分
261
发表于 2021-10-24 12:22:09 | 显示全部楼层
激动人心,无法言表!
工控课堂 www.gkket.com

13

主题

396

回帖

2653

积分

高级会员

积分
2653
发表于 2021-10-26 05:16:46 | 显示全部楼层
真是难得给力的帖子啊。
工控课堂 www.gkket.com

0

主题

104

回帖

162

积分

新手上路

积分
162
发表于 2025-11-17 01:19:28 | 显示全部楼层
楼主辛苦啦,期待下一篇分享!
工控课堂 www.gkket.com

0

主题

142

回帖

261

积分

注册会员

积分
261
发表于 2025-11-17 02:09:26 | 显示全部楼层
楼主太会说了,字字句句都在理~
工控课堂 www.gkket.com

0

主题

48

回帖

85

积分

新手上路

积分
85
发表于 2025-11-17 04:44:18 | 显示全部楼层
占个楼慢慢看,先马克一下
楼主辛苦啦,期待下一篇分享
工控课堂 www.gkket.com

0

主题

220

回帖

723

积分

中级会员

积分
723
发表于 2025-11-17 04:51:06 | 显示全部楼层
理性围观,感觉大家说得都有道理
工控课堂 www.gkket.com

0

主题

88

回帖

129

积分

新手上路

积分
129
发表于 2025-11-17 04:51:55 | 显示全部楼层
浅蹲一个后续,楼主更新踢我一下
工控课堂 www.gkket.com

0

主题

84

回帖

134

积分

新手上路

积分
134
发表于 2025-11-17 04:53:03 | 显示全部楼层
同款经历!简直是世另我
工控课堂 www.gkket.com

0

主题

87

回帖

134

积分

新手上路

积分
134
发表于 2025-11-17 04:53:04 | 显示全部楼层
救命!这回复笑到我捶桌,必须置顶~
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-23 08:13 , Processed in 0.118407 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表