无法让Get ajax工作

时间:2013-10-18 14:52:22

标签: jquery ajax get

当我查看Chrome检查器中test.php发送的标题时,它显示action : startjson但我无法检索此变量$_GET['action'],数组为空。

test.php:

<body>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

    <script language="JavaScript">
    $(document).ready(function(){

    $("#testjson").click(function(e){
        startJsonSession();
        return false;
    });

    function startJsonSession(){  
        $.ajax({
            url: "test2.php?action=startjson",
            cache: false,
            dataType: "json",
            complete: function(data) {
                username = data.username;
                alert(username);
            }

    });
    }

    }); 
    </script>
    <button id="testjson">
    toto
    </button>
</body>

test2.php:

    <?php 
    if ($_GET['action'] == "startjson") { 
            startjsonSession(); 
        } 

        function startjsonSession() {
            $items = '';

            print json_encode(array(
                "username" => "bob",
                "items" => array(
                    "item1" => "sandwich",
                    "item2" => "applejuice"
                )
            ));
        }

1 个答案:

答案 0 :(得分:1)

成功与完成之间存在差异,完整功能不会收到'数据'参数,因此如果您的操作取决于数据,那么它将无法在那里工作。

完成:当请求完成时,无论是失败还是成功,都会触发完整的回调选项。它接收jqXHR对象,以及包含成功或错误代码的字符串。

成功:如果请求成功,则调用回调选项。它接收返回的数据,包含成功代码的字符串和jqXHR对象。