有没有一种方法,使一个HTTP请求使用Chrome开发工具,而不使用插件像海报?
当前回答
如果你在你的网站上使用jquery,你可以在你的控制台上使用这样的东西
$ . post ( “dom / data-home.php”, { 类型:"home", id: "0" }、功能(数据){ console.log(数据) }) < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js " > < /脚本>
其他回答
我知道,老帖子……但把这个留在这里可能会有帮助。
现代浏览器现在支持Fetch API。
你可以这样使用它:
fetch("<url>")
.then(data => data.json()) // could be .text() or .blob() depending on the data you are expecting
.then(console.log); // print your data
ps:它将进行所有的CORS检查,因为它是一个改进的XmlHttpRequest。
如果你在你的网站上使用jquery,你可以在你的控制台上使用这样的东西
$ . post ( “dom / data-home.php”, { 类型:"home", id: "0" }、功能(数据){ console.log(数据) }) < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js " > < /脚本>
要GET带报头的请求,请使用此格式。
fetch('http://example.com', {
method: 'GET',
headers: new Headers({
'Content-Type': 'application/json',
'someheader': 'headervalue'
})
})
.then(res => res.json())
.then(console.log)
最短的方法是:
await (await fetch('<URL>')).json()
使用现代async/await javascript sintax,你可以做到如下所示。
const options = {method: 'GET', Content-Type: 'application/json'};
let res = await fetch('https://yourdomain.com/somedata.json', options);
let json = await res.json();
它将进行所有CORS检查(跨原产地资源共享)。如果web服务器已经允许来自所有域的CORS,你就准备好了。如果你需要在web服务器上启用CORS,请遵循以下两种情况:一种是nginx,另一种是node express。
与NGINX兼容
从所有网站启用CORS 如果您希望对所有网站启用CORS,即接受所有网站的跨域请求,请添加以下内容
add_header Access-Control-Allow-Origin *;
从一个域启用CORS
add_header Access-Control-Allow-Origin "stackoverflow.com";
使用Express启用CORS
对于node,它只是安装cors与yarn添加cors,然后使用它
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
推荐文章
- 资源解释为样式表,但以MIME类型text/html传输(似乎与web服务器无关)
- Chrome iOS(和Safari)的远程调试
- 在Chrome中模拟有限的带宽?
- 资源解释为文档,但使用MIME类型application/zip传输
- 如何让Chrome允许混合内容?
- 了解Chrome网络日志“停滞”状态
- 如何卸载Service Worker?
- jQuery ' .is(":visible") '在Chrome中无效
- 如何保存样式面板的CSS Chrome开发工具的变化?
- 当你双击日文文本时,Chrome如何决定突出显示什么?
- Chrome不会删除会话cookie
- 在Chrome和Firefox中用Javascript报告的神秘的“脚本错误”
- 在Window上执行'btoa'失败:要编码的字符串包含Latin1范围之外的字符。
- Chrome中的请求监控
- 在Chrome 55中,防止显示HTML 5视频的下载按钮