For example in a webpage I have somewhere:
<script id="ajaxify-data" type="application/json">
{"key1":123,"key2":333}
</script>
Can I use javascript to parse it? (specifically, the ajaxify-data)
jQuery $("#ajaxify-data") just doesn't work here..
Edit: The website that I wanted to crawl actually is more complex than the simple example given above...
https://discuss.leetcode.com/unread looks like the web source, although containing ajaxify-data tag, it actually renders the data in ajax result instead of storing it in the actual ajax tag above.
This is what I get in the console:
> document.getElementById('ajaxify-data')
null
The webpage screenshot:

Just JSON parse the innerHTML of the script tag.
Plain JavaScript:
var json = JSON.parse(
document.getElementById('ajaxify-data').innerHTML
);
console.log(json);
console.log(json.key1);
<script id="ajaxify-data" type="application/json">
{ "key1": 123, "key2": 333 }
</script>
Or with jQuery:
var json = JSON.parse($('#ajaxify-data').html())
console.log(json);
console.log(json.key1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="ajaxify-data" type="application/json">
{ "key1": 123, "key2": 333 }
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With