【发布时间】:2018-01-12 06:55:09
【问题描述】:
我正在对一些 API 代码进行一些故障排除。
我们希望从 API 调用中带回的 XML 格式中获取并将其转换为 JSON。为了帮助我完成这个操作,我想看看数据是什么样子的,所以我只是想让它打印到 HTML 文档的正文中。这是在浏览器中打开时什么都不显示,但显示响应数据的 HTML 代码:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<h2> Response Data </h2>
<!-- AJAX API Call -->
<script>
{
var settings = {
"async": true,
"crossDomain": true,
"url": "https://qualysapi.qg2.apps.qualys.com/api/2.0/fo/scan/?action=list",
"method": "GET",
"headers": {
"Authorization": "#############################################",
"Cache-Control": "no-cache",
}
}
$.ajax(settings).done(function (response) {
$('body').append(response);
});
}
</script>
</body>
有没有更好的方法来做到这一点?
【问题讨论】:
-
当你使用
console.log时它不会显示在页面上,它会打印到控制台(F12,控制台选项卡) -
我不确定这是否是由于未获得授权,但我收到了 302 redirect。
Be sure to specify the header with your API request. This syntax must be included: "-H X-Requested-With: <user description, like a user agent>" -
@showdev,这可能是因为出于安全原因,我将授权更改为 ######。
-
@PatrickEvans 实际上我之前在控制台中查看过是否有错误。我没有看到任何显示或有错误
-
@NJ13 同意。不幸的是,如果没有身份验证,这将很难解决。
标签: javascript html json ajax