找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2000|回复: 12

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

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

2540

主题

1355

回帖

2万

积分

管理员

积分
21304
发表于 2021-10-5 15:49:49 | 显示全部楼层 |阅读模式
实现功能:
实际项目中需要在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站测试页


工控课堂 www.gkket.com

0

主题

189

回帖

452

积分

注册会员

积分
452
发表于 2021-10-5 15:49:51 | 显示全部楼层
感恩无私的分享与奉献
工控课堂 www.gkket.com

0

主题

251

回帖

484

积分

注册会员

积分
484
发表于 2021-10-5 16:20:51 | 显示全部楼层
我十目一行也还是看不懂啊。
工控课堂 www.gkket.com

0

主题

59

回帖

77

积分

新手上路

积分
77
发表于 2025-11-17 01:30:14 | 显示全部楼层
学到干货了,感谢分享,已火速收藏
工控课堂 www.gkket.com

0

主题

120

回帖

376

积分

注册会员

积分
376
发表于 2025-11-17 03:46:57 | 显示全部楼层
路过打卡,为优质内容疯狂打 call
工控课堂 www.gkket.com

0

主题

84

回帖

127

积分

新手上路

积分
127
发表于 2025-11-17 04:11:00 | 显示全部楼层
笑不活了,评论区比正文还精彩
已转发给朋友,一起快乐一下
工控课堂 www.gkket.com

0

主题

91

回帖

139

积分

新手上路

积分
139
发表于 2025-11-17 07:16:29 | 显示全部楼层
打卡路过,支持优质原创内容~
工控课堂 www.gkket.com

0

主题

74

回帖

111

积分

新手上路

积分
111
发表于 2025-11-17 13:18:26 | 显示全部楼层
水个经验,支持楼主,加油呀
工控课堂 www.gkket.com

0

主题

86

回帖

126

积分

新手上路

积分
126
发表于 2025-11-17 13:24:57 | 显示全部楼层
水个经验,楼主加油,支持你~
工控课堂 www.gkket.com

0

主题

81

回帖

103

积分

新手上路

积分
103
发表于 2025-11-17 22:55:05 | 显示全部楼层
原来还有这种操作,长见识了!
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

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

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

GMT+8, 2025-12-23 06:05 , Processed in 0.262058 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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