1

Here is my code:

$( document ).ready(function() {

    $.getJSON('onlineinsurance/services/aggregateRating.php', { rating: false } , function(result) {
        $("#rating-value").text(result['avg_rating']);
        $("#rating-count").text(result['count']);
        schema_org = ` <script type="application/ld+json">
        {
            "@context": "http://schema.org/",
            "@type": "Product",
            "name": "Some name",
            "image": "some image path",
            "description": "some description",
            "brand": "some brand name",
            "offers": {
            "@type": "AggregateOffer",
            "priceCurrency": "BGN",
            "lowPrice": "176.93",
            "offerCount": 10
            },
            "aggregateRating": {
                "@type": "AggregateRating",
                "ratingValue": "'` + result['avg_rating'] + `'",
                "reviewCount": "'` + result['count'] + `'"
            }
        }
    </script>
    `;
        $("#schema-org").after(schema_org);
    });
}); 

And HTML:

<p id="schema-org"></p> 

I cannot handle from where problem comes, If I change this row:

$("#schema-org").after(schema_org);

to

$("#schema-org").after('test');

for example, it works perfect, so I think that multiply rows string is the problem? Am I wrong?

EDIT: Okay .. major problem was in me, when I ispect with cntrl + u the string doesn't appear but when I inspect with f12 it is there!

Sahin Urkin
  • 289
  • 1
  • 8

3 Answers3

0

Could you just create a <SCRIPT> jQuery element and just set the text value?

Also, since it is JSON, you could just serialize it into a string if you wanted.

Additionally, you can skip concatenation and use placeholders in your template literal. e.g. ${result['avg_rating']}

JSON.stringify(jsonObj, null, 2);

let result = {}; // Avoid error below...

let schema_org = `{
  "@context": "http://schema.org/",
  "@type": "Product",
  "name": "Some name",
  "image": "some image path",
  "description": "some description",
  "brand": "some brand name",
  "offers": {
    "@type": "AggregateOffer",
    "priceCurrency": "BGN",
    "lowPrice": "176.93",
    "offerCount": 10
  },
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "'${result['avg_rating']}'",
    "reviewCount": "'${result['count']}'"
  }
}`;

$("#schema-org").after($('<script>').attr('type', 'application/ld+json').text(schema_org));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="schema-org"></div>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • I'm not sure why this is supposed to fix the problem - what OP has should also work, even if it's not a good practice. – VLAZ Sep 25 '19 at 12:12
0

Can you try replacing your schema_org value with this?

` <script type="application/ld+json">
    {
        "@context": "http://schema.org/",
        "@type": "Product",
        "name": "Some name",
        "image": "some image path",
        "description": "some description",
        "brand": "some brand name",
        "offers": {
        "@type": "AggregateOffer",
        "priceCurrency": "BGN",
        "lowPrice": "176.93",
        "offerCount": 10
        },
        "aggregateRating": {
            "@type": "AggregateRating",
            "ratingValue": "'${result['avg_rating']}'",
            "reviewCount": "'${result['count']}'"
        }
    }
<\/script>
`;

And try. I think the issue is that your script is being picked up properly

Abishek Aditya
  • 802
  • 4
  • 11
  • Forward slash is not a special character in template literals, so no need to escape it. The expression interpolation is better to be used than just concatenation but that also shouldn't break anything. – VLAZ Sep 25 '19 at 12:19
-1

Your closing script tag is breaking the string, using escape character for this will make it work.

So, it's not about multi-line strings.

Here is an example

$( document ).ready(function() {
var schema_org = ` <script type="application/ld+json">
    {
        "@context": "http://schema.org/",
        "@type": "Product",
        "name": "Some name",
        "image": "some image path",
        "description": "some description",
        "brand": "some brand name",
        "offers": {
        "@type": "AggregateOffer",
        "priceCurrency": "BGN",
        "lowPrice": "176.93",
        "offerCount": 10
        },
        "aggregateRating": {
            "@type": "AggregateRating",
            "ratingValue": "avg_rating",
            "reviewCount": "count"
        }
    }
<\/script>
`;
    $("#schema-org").after(schema_org);
});
  • "*Your closing script tag is breaking the string*" how is it breaking the string? Forward slashes are not special in a template literal – VLAZ Sep 25 '19 at 12:31