且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何外部化json-ld并包含在html文档中

更新时间:2023-12-04 21:49:40

你不能那样做。您应该使用 AJAX 请求获取 json 使用 jQuery

JS

  $(函数(){
$ .getJSON(data123.jsonld,函数(数据){
$('。json').text(data);
});
});

HTML

 < div class =json>< / div> 

如果您的json文件不在您的文件系统中(跨域)你应该尝试像


Is it possible to externalize json-ld and include it in an html document like this:

<script type="text/javascript" src="http://www.example.com/data123.jsonld"></script>

There doesn't seem to be any documentation about this online....

You can't do that. You should get the json with an AJAX request.

You can do it easy with jQuery

JS

$(function(){
  $.getJSON("data123.jsonld", function(data) {
      $('.json').text(data);   
  });
});

HTML

 <div class="json"></div>

If your json file is not in your file system (cross domain) you should try something like this.