iframe中的Ajax不更新iframe

时间:2014-07-08 00:35:33

标签: javascript php jquery ajax iframe

我已经搜索了这个问题的答案,但尚未找到类似的问题或答案。如果这是一个"重新询问"我很抱歉。

基本上我的问题是这样的:我的父页面里面有一个iframe,而iframe里面是一个包含我的.js文件的php页面,其中包含我的AJAX。我想在iframe中点击按钮来创建AJAX请求并更新iframe。据我所知,我已经正确设置了但是当我试图回应我的$ _POST(我也试过GET)变量来测试它们时它们永远不会改变。这是我的代码:

父:

<!DOCTYPE html>
<html style = "height: 100%; width: 100%;">
    <head>
        <title>Employee Web Service</title>
        <link rel="stylesheet" type="text/css" media="screen, projection" href="lib/css/main.css" />
        <script src="lib/scripts/jquery-2.0.3.min.js" type="text/javascript"></script>
    </head>
    <body style = "height: 100%;">
        <div id = "header">
            <img src = "images/header.png" id = "headerLogo">
        </div>
        <div id = "nav">
            <ul>
                <li><a href = "home.php" target = "content">Home</a></li>
                <li><a href = "#">Modules</a>
                    <ul>
                        <li><a href = "test.php?id=1" target = "content">Module 1</a></li>
                        <li><a href = "test.php?id=2" target = "content">Module 2</a></li>
                        <li><a href = "test.php?id=3" target = "content">Module 3</a></li>
                        <li><a href = "test.php?id=4" target = "content">Module 4</a></li>
                    </ul>
                </li>
                <li><a href = "scores.php" target = "content">Scores</a></li>
                <li><a href = "calendar.php" target = "content">Calendar</a></li>
                <li><a href = "logout.php">Logout</a></li>
            </ul>
        </div>
        <div id = "content">
            <iframe src = "home.php" name = "content" id = "frameContent"></iframe>
            <script>
                $(document).ready(function(){
                    $('#frameContent').load(function(){
                        $('#frameContent').contents().find('head').append('<link href="lib/css/iframe.css" rel="stylesheet" type="text/css" />');
                    });
                });
            </script>
        </div>
    </body>
</html>
iframe中的

test.php:

<?php
    if(isset($_POST['begin'])){
        $test = true;
        echo($_POST['begin']);
    } else if (isset($_POST['backToSlides'])){
        echo($_POST['backToSlides']);
    }
?>
<head>
    <script src="lib/scripts/jquery-2.0.3.min.js" type="text/javascript"></script>
    <script src = "lib/scripts/testScript.js" type="text/javascript"></script>
</head>
<body>
    <div id = "startTest" <?php if(isset($test)){echo 'style = "visibility: hidden; disabled: true;"';}?> >
        <h1>Test <?php echo $id; ?></h1>
        <form id = "begin">
            <input type = "submit" name = "backToSlides" value = "Go back" style = "background: #B9090B;">
            <input type = "submit" name = "begin" value = "Start Test" style = "background: #09913A;">
        </form>
    </div>
    <div id = "testContent"></div>
</body>

testScript.js:

$(document).ready(function(){
    $('input[name = "backToSlides"], input[name = "begin"]').click(function(event){
        event.preventDefault();

        post = $(this).attr("name") + "=" + $(this).val();
        $.ajax({
            url: "/test.php",
            type: "POST", 
            data: post, //Data purposely not serialized due to the way submit-type input field values are handled
            success: function(){
                console.log("Processing request...");},
            error: function(){
                console.log("Error submitting request");}
        });
    });
});

此ALWAYS调用成功函数,但我从未在post数据中看到提交按钮的值。我知道他们正在被正确地传递到ajax呼叫,因为我已经尝试呼叫提醒($(this).attr(&#34; name&#34;)+&#34; =&#34; + $(this).val())我得到&#34; key = value&#34;的预期结果。

非常感谢快速响应!

2 个答案:

答案 0 :(得分:1)

您需要阻止默认浏览器提交表单,否则因为您要提交相同的网址,它只会刷新iframe页面

$('input[name = "backToSlides"], input[name = "begin"]').click(function(event){

    event.preventDefault();

    /* ajax */

})

答案 1 :(得分:0)

将iframe更改为div并改为使用jquery .load()函数

父页面上的

<script>
    $(document).ready(function(){
        $("a").click(function(event){
            event.preventDefault();
            tar = $(this).attr("href");
            $("#content").load(tar);
        );
    );
</script>

我可以将所有<a>标记转换为<div>标记,将光标更改为鼠标悬停时的指针,这样我就不必阻止默认操作并扫描'href '属性,但现在这就行了。