从另一个窗口成功调用ajax文档可以通过以下步骤实现:
以下是一个示例代码:
目标窗口(target.html)中的代码:
<!DOCTYPE html>
<html>
<head>
<title>目标窗口</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function handleResponse(data) {
// 处理ajax请求的回调函数
console.log(data);
}
function sendAjaxRequest() {
$.ajax({
url: 'ajax_document.html', // 请求的URL
type: 'GET', // 请求类型
success: handleResponse // 回调函数
});
}
</script>
</head>
<body>
<button onclick="sendAjaxRequest()">发送Ajax请求</button>
</body>
</html>
当前窗口中的代码:
<!DOCTYPE html>
<html>
<head>
<title>当前窗口</title>
<script>
function callAjaxInTargetWindow() {
var targetWindow = window.open('target.html'); // 打开目标窗口并保存引用
targetWindow.handleResponse('Hello from current window!'); // 调用目标窗口中的函数
}
</script>
</head>
<body>
<button onclick="callAjaxInTargetWindow()">调用目标窗口中的函数</button>
</body>
</html>
在当前窗口中点击"调用目标窗口中的函数"按钮,将会打开目标窗口,并在控制台输出"Hello from current window!"。这样就成功地从另一个窗口调用了ajax文档。
领取专属 10元无门槛券
手把手带您无忧上云