如何编写一个HTML JSON AJAX测试工具与美化,语法突出显示JSON结果
How to write an HTML JSON AJAX test harness with prettify, syntax highlighted JSON result?
我已经写了一个JSON API,但是我不会在视图上工作。
我如何测试JSON API与一个简单的网页与美化,语法突出显示JSON结果?
让我们使用以下GET API调用作为示例:
http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full ? alt = json
注意:这个问题是指导性的,会有答案的。
这是整个HTML文件的内联javascript。
我在解决方案中使用了jQuery和highlight.js
我在Chrome上运行结果,我不相信它在IE中正常工作。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/7.3/highlight.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/7.3/styles/default.min.css" />
<script>
$(document).ready(function() {
hljs.initHighlightingOnLoad();
$.ajax({
url: "http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full",
dataType: "json",
//contentType: "application/json; charset=utf-8", // NOTE: use this parameter when calling your host server, but doesn't work with this google api
type: "GET",
data : { alt: "json" },
complete: function(xhr, textStatus) {
// set status
$("#status").html(textStatus);
// set plaintext
$("#result").val(xhr.responseText);
// set prettytext
var data = JSON.parse(xhr.responseText);
var stringify = JSON.stringify(data, undefined, 2);
var prettify = hljs.highlightAuto(stringify).value;
prettify = hljs.fixMarkup(prettify);
$("#prettyResult").html(prettify);
}
});
});
</script>
</head>
<body>
<tt>Status: <span id="status">waiting ...</span></tt><br /><br />
<textarea id="result" style="width: 1024px; height: 100px;"></textarea>
<pre><code id="prettyResult" class="json" style="width: 1024px;"></code></pre>
</body>
</html>
相关文章:
- 使用 AJAX 向 Flask 发送 JSON 语法错误
- C#中的JSON语法
- 这是正确的json语法吗
- JSON 语法错误:意外的令牌<
- JSON 语法错误:未捕获语法错误:意外令牌 [.
- JQuery 在使用 PHP 的 json_encode 时给出 JSON 语法错误
- 如何在对象内部获取数组,删除双引号并将其作为对象放入 JSON 语法中
- JSON语法错误..我没有'我看不见
- 仅firefox中存在JSON语法错误
- 使用JSON语法将参数作为值发送
- JSON语法-变量被解释为文字
- 循环的JavaScript json语法错误
- js-object-to-json语法错误
- 理解json语法的问题
- IE8中的JSON语法错误
- JSON语法帮助
- 解析函数以字符串形式存储在对象文本/JSON语法中,并进行区分
- 这个JSON语法有什么问题
- Json语法有什么问题?
- JavaScript JSON语法错误:无法识别的表达式