如何将JSON-LD从内联移动到文件中? [英] How to move JSON-LD from in-line to in-a-file?
本文介绍了如何将JSON-LD从内联移动到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML
文档,其中JSON-LD
发布如下:
I have an HTML
document with JSON-LD
published as follows:
<script type="application/ld+json">
{
"@context": {
"gs1": "http://vocab.gs1.org/v1#",
"xsd":"http://www.w3.org/2001/XMLSchema#"
},
"@id": "retailer:12345",
"@type": "gs1:Offering",
"gtin13":"5011476100111",
"gs1:hasPrice": {
"gs1:price": "2.49",
"gs1:priceTypeCode": "USD",
"@type":"gs1:Price"
}
}
</script>
这可以按预期方式工作(这意味着它有效并且可以使用Google Structured Data Testing Tool
和Structured Data Linter
看到)
This works as expected (meaning it is valid and visible using Google Structured Data Testing Tool
and Structured Data Linter
)
使用Same Origin
(不是不是 Cross Origin
),如何将嵌入式JSON-LD
移至文件?
Using Same Origin
(not Cross Origin
), how to move the in-line JSON-LD
to a file?
此方法失败:
<script type="application/ld+json" src="_URL_.json"></script>
此方法也失败:
<script type="application/ld+json">
$(document).ready(function(){
$.ajax({
url: "_URL_.json",
dataType: 'json',
});
});
</script>
如何更正我的方法来创建解决方案,其中JSON-LD在服务器上的文件中,而不是HTML中的文件中?
接下来,假设我的网站启用了CORS.
Next, assume my site is CORS-enabled.
如何以JSONP格式发布JSON-LD结构,以便另一个域可以订阅JSON-LD数据?
推荐答案
您可以动态生成脚本标记:
You could generate your script tag dynamically:
<script>
// do some ajax, get the JSON-LD as `data` and then...
$.ajax(...).done(function(data) {
var el = document.createElement('script');
el.type = 'application/ld+json';
el.text = JSON.stringify(data);
document.querySelector('body').appendChild(el);
});
</script>
这篇关于如何将JSON-LD从内联移动到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文