[英]application/ld+json and javascript data exchange
I have some jQuery code which constructs an array called myList[]
. 我有一些jQuery代码构造一个名为myList[]
的数组。 This array appears in the console like this: ["2 items", "3items", "so on"]
, so this part goes well. 这个数组出现在控制台中,如下所示: ["2 items", "3items", "so on"]
,所以这部分进展顺利。
<script type="text/javascript">
var myList = [];
function buld myList(){
...
}
I need to pass myList[]
to the application/ld+json
like 我需要将myList[]
传递给application/ld+json
类的
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Recipe",
"recipeIngredients": myList, //this one doesn't work
}
.. ..
How can I pass the values from javascript to the application/ld+json
? 如何将值从javascript传递给application/ld+json
? Thanks in advance! 提前致谢!
Please try this: 请试试这个:
<script id="myJSONID" type="application/ld+json"></script>
Then: 然后:
var myList = [];
function buildMyList() {
return ["2 items", "3items", "so on"];
}
$("#myJSONID").text(function() {
return JSON.stringify({
"@context": "http://schema.org/",
"@type": "Recipe",
"recipeIngredient": buildMyList()
});
});
Or: 要么:
<script type="text/javascript">
var myList = [];
function buildMyList() {
return ["2 items", "3items", "so on"];
}
var el = document.createElement('script');
el.type = 'application/ld+json';
el.text = JSON.stringify({
"@context": "http://schema.org/",
"@type": "Recipe",
"recipeIngredient": buildMyList()
});
document.querySelector('body').appendChild(el);
</script>
Demo: https://jsfiddle.net/iRbouh/9po7dtg4/ 演示: https : //jsfiddle.net/iRbouh/9po7dtg4/
Note: Please make sure you change recipeIngredients
to recipeIngredient
, singular. 注意:请确保将recipeIngredients
更改为recipeIngredient
,singular。 (Thank you @AlexKudryashev). (谢谢@AlexKudryashev)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.