javascript - 在iframe中使用`window.top.$`发送ajax请求,但是referer不是iframe src的url
问题描述
有两个 html 文件,一个index.html
和inner.html
.
** index.html **
<html>
<body>
<script src="./node_modules/jquery/dist/jquery.js"></script>
<iframe src="./inner.html" frameborder="1"></iframe>
</body>
</html>
它引用了jquery。
** 内部.html **
<html>
<body>
<input type="button" id="button" value="Get Data by AJAX">
<script src="./inner.js"></script>
</body>
</html>
** 内部.js **
const $ = window.top.$
$('#button', window.document).click(function () {
$.ajax({
url: '/data.json',
success: function (data) {
console.log(data)
},
context: this
})
})
在服务器端,我将打印 header referer
。
打开 index.html 的 url 是:http://localhost:3000
.
我发现当我在 inner.html 中使用$
from发出 ajax 请求时window.top
,referer
在 server 中打印的是http://localhost:3000
,这是外部页面的 url。
我希望成为 iframe 的 url http://localhost:3000/inner.html
,但是如果我坚持使用来自window.top
.
PS:如果我将代码<script src="./node_modules/jquery/dist/jquery.js"></script>
从移动index.html
到inner.html
,并用于window.$
在 inner.html 中发送 ajax 请求,那么引用者就是我所期望的:http://localhost:3000/inner.html
这是我项目的一个简化demo,其中我们要使用平台提供的jquery,即window.top.$
解决方案
推荐阅读
- sql-server - MSSQL Server ',' 附近的语法不正确
- angular - 为什么我必须将 @angular/common 管道放入 providers 数组中?
- javascript - 单击复选框将项目添加到单独的 div 一次只显示一个项目
- html - 使用 v-else-if 和事件 VueJS
- angular - 失败:模板解析错误:没有将“exportAs”设置为“ngForm”Angular 6 的指令
- vb.net - 将遗留代码转换为参数化查询的问题
- facebook - Facebook Graph API 点赞/评论业务页面推荐
- javascript - 如何阻止模式直到解决 api 请求
- ios - 使用 UIBezierPath bezierPathWithRoundedRect 的单边角半径:得到扭曲的角。任何人都可以帮助实现良好的角落吗?
- windows - 从 IShellView 实例获取列表视图控件句柄