如何使用 javascript 打印 http 响应中返回的内容
How to print content that is returned in http response using javascript
我想在控制台中打印 http 响应中返回的内容。例如,当我转到 https://google.com 并在“网络”选项卡中时,我可以看到脚本、文本、文档、png 等内容。我想打印所有 .png 文件的名称。
我尝试使用类似的东西:
function hand () {
console.log(this.getResponseHeader('content-type'));
}
var x = new XMLHttpRequest();
x.open('GET', 'https://google.com', true);
x.onreadystatechange = hand;
x.send();
但这对我不起作用。下面这个动作是为我页面上的按钮指定的。
跨域请求只需are not allowed by default. The remote server may provide permission to your application through CORS or by supporting Ajax alternatives like JSONP。
已编辑:
使用 AJAX 获取跨域数据的唯一(简单)方法是使用服务器端语言作为代理,如 Andy E 所述。这是一个如何使用 jQuery:
实现的小示例
jQuery部分:
$.ajax({
url: 'proxy.php',
type: 'POST',
data: {
address: 'http://www.google.com'
},
success: function(response) {
// response now contains full HTML of google.com
}
});
就这么简单。请注意您可以或不能对抓取的数据做什么,并非常清楚这样的代理是一个严重的安全漏洞。至少列出一个可接受的地址列表,不要盲目接受任何通过的地址。在这里查看一个不错的代理脚本:PHP Simple Proxy
我想在控制台中打印 http 响应中返回的内容。例如,当我转到 https://google.com 并在“网络”选项卡中时,我可以看到脚本、文本、文档、png 等内容。我想打印所有 .png 文件的名称。 我尝试使用类似的东西:
function hand () {
console.log(this.getResponseHeader('content-type'));
}
var x = new XMLHttpRequest();
x.open('GET', 'https://google.com', true);
x.onreadystatechange = hand;
x.send();
但这对我不起作用。下面这个动作是为我页面上的按钮指定的。
跨域请求只需are not allowed by default. The remote server may provide permission to your application through CORS or by supporting Ajax alternatives like JSONP。
已编辑:
使用 AJAX 获取跨域数据的唯一(简单)方法是使用服务器端语言作为代理,如 Andy E 所述。这是一个如何使用 jQuery:
实现的小示例jQuery部分:
$.ajax({
url: 'proxy.php',
type: 'POST',
data: {
address: 'http://www.google.com'
},
success: function(response) {
// response now contains full HTML of google.com
}
});
就这么简单。请注意您可以或不能对抓取的数据做什么,并非常清楚这样的代理是一个严重的安全漏洞。至少列出一个可接受的地址列表,不要盲目接受任何通过的地址。在这里查看一个不错的代理脚本:PHP Simple Proxy