Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

jsonp - How to move JSON-LD from in-line to in-a-file?

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>

This works as expected (meaning it is valid and visible using Google Structured Data Testing Tool and Structured Data Linter)

Using Same Origin (not Cross Origin), how to move the in-line JSON-LD to a file?

This approach fails:

<script type="application/ld+json" src="_URL_.json"></script>

This approach also fails:

<script type="application/ld+json">
        $(document).ready(function(){
            $.ajax({
                url: "_URL_.json",
                dataType: 'json',
                });        
        });
</script>

How to correct my approach to create a solution where the JSON-LD is in file on my server, not in the HTML?

Next, assume my site is CORS-enabled.

How to publish the JSON-LD structure in a JSONP format so that another domain can subscribe to the JSON-LD data?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Tools like the Google Structured Data Testing Tool or the Structured Data Linter don’t support external references. And probably many consumers of your JSON-LD neither.

If you want to do it anyway, you would have to use a link instead of a script element, because for the use as data block, the script element may not have a src attribute.

The variant with including the JSON-LD dynamically might work for consumers that support JavaScript for that purpose. (Google seems to support it; maybe it’s only their testing tool that can’t use it.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...