我想将此 JSON URL 提供给另一个网站
http://naturetrek.co.uk/blog/api/get_recent_posts/
但问题是因为前三个<p>
标记其无效 JSON,您可以在此处检查...
如果你删除这3个标签,它就变得有效。
我们无法更改 Naturetrek 博客的 JSON 字符串输出。
在我们自己的代码中,我们使用以下内容......
<h2>Naturetrek BLOG</h2>
<div id="ntblog"></div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var blogURL = "http://naturetrek.co.uk/blog/api/get_recent_posts/";
$.getJSON(blogURL, function(info) {
alert("here");
var output = info.status;
/*
for (var i = 0; i <= info.posts.length - 1; i++) {
output += '<li>' +
'<a href = "' + info.posts[i].url +
'">' + info.posts[i].title + '</a></li>';
}
*/
var ntblog = document.getElementById('ntblog');
ntblog.innerHTML = output;
});
});
</script>
警报永远不会被调用,因为 JSON 是无效的 JSON。我的问题是——有没有办法准备 JSON 来删除这三个 <p>
标签,不知何故?或者你能想到的其他方式?
最佳答案
$.getJSON 只是以下内容的简写:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
请参阅此处的文档:http://api.jquery.com/jQuery.getJSON/
只需使用 $.ajax 将 dataType 更改为“html”,并使用 $.parseJSON 在 success 函数中解析修改后的 JSON 数据
注意:由于您似乎正在执行跨域请求,因此您必须使用 jsonp,请在此处阅读更多内容:
关于javascript - 如何预解析 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20325267/