Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access JSON from an iframe originating from same domain?

In my webpage a hidden iframe is loaded with some JSON in it. This JSON is refreshed by some actions on the page. How do I access this JSON in iframe from my web page? for some unknown arcane unexplainable reason I am forced to use jQuery 1.3.2. so no $.parseJSON()

like image 355
Kumar Avatar asked Jul 30 '11 03:07

Kumar


3 Answers

I think you can use:

var json = $.parseJSON($("#hiddeniframe").contents().text());

Something along those lines will work at least.

like image 103
Paul Avatar answered Oct 16 '22 21:10

Paul


All modern browsers include a JSON parsing library:

var data = JSON.parse($("#hiddeniframe").contents().text());

If you need to support older browsers there are several libraries to choose from that will provide the same interface. The better ones will check to see if the browser is providing a native implementation and not override it, since it's bound to be faster.

See also JSON.stringify()

like image 8
db48x Avatar answered Oct 16 '22 20:10

db48x


The code @Paulpro posted:

var json = $.parseJSON($("#hiddeniframe").contents().text());

doesn't work for me.

I changed the code to:

var json = $.parseJSON($("#hiddeniframe").contents().find("*").first().text());

And now it works.

like image 1
Evi Song Avatar answered Oct 16 '22 20:10

Evi Song