开发者

Echoing json into a page used as a same-origin iframe does not give expected result

开发者 https://www.devze.com 2023-03-26 03:27 出处:网络
I have a trouble with Javascript and PHP: I created a page like this: <html> <body> <iframe id="a" src="iframe.php" width="500" height="200">&

I have a trouble with Javascript and PHP: I created a page like this:

<html>
<body>
<iframe id="a" src="iframe.php" width="500" height="200"></iframe>
</body>
</html>

and inside iframe.php, I just echo the JSON string:

<?php
$json = array(
    'status' => 'error',
    'html' => '<span class="cool">This is a string</span>'
);

echo json_encode( $json );

What I want is getting the correct raw string echoed by iframe.php using Javascript (maybe jQuery), and parse it as an JSON object.

But the problem is that I always get the wrong cont开发者_JAVA百科ent. I tried both the method described here and jQuery('iframe').contents().find('body').html(), but no luck. The span tag is missed or not formatted correctly.


<script>
    var frameId = 'a'; // the frame id in your sample code
    var frameHtml = frames[frameId].document.documentElement.innerHTML;
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号