$ .get无效

时间:2012-10-22 21:14:11

标签: php jquery

我不知道这里有什么问题我试过this link知道如何在php和jquery之间进行交互

这是我的代码,但它会提醒一个空盒子,我确定它无法读取test.php,但我不知道为什么?!

mytest.php

<?php
if($_GET['cookie']!='')
{
    setcookie("cookie", $_GET['cookie']);
    echo 'set cookie: ' . $_GET['cookie'];
}
else
    echo 'get cookie: ' . $_COOKIE["cookie"];

?>

的test.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>Ajax Set &amp; Get Cookie</title>
  <script type="text/javascript" src="jquery.min.js"></script> 

<script type="text/javascript">

$(document).ready( function() {

        $("#setcookie").click(function() {
            if($("#cookievalue").val() != '')
                setCookie( $("#cookievalue").val() );
        });

        $("#getcookie").click(function() {
            getCookie();
        });

     });

function setCookie(value)
{

    $.get("mytest.php", { cookie: value},
        function(data){
            alert(data);
        }
    );
}

function getCookie(value)
{

    $.get("mytest.php", { },
        function(data){
            alert(data);
        }
    );
}

</script> 

</head>

<body>
<div align="center">

    Cookie: <input type="text" id="cookievalue" value="" />
    <br />
    <input type="button" id="setcookie" value="Set Cookie"  />
    <input type="button" id="getcookie" value="Get Cookie"  />

</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要将cookie添加到查询字符串

$.get("mytest.php?cookie=12345"