Ajax发布到php。保存cookie并将其发送回javascript

时间:2015-08-04 23:52:53

标签: javascript php jquery ajax cookies

我通过ajax POST向php发送数据。我想将它保存在php cookie中,然后能够通过ajax GET请求在javascript中检索它。在PHP中,我的cookie检查功能返回“cookie ____未设置”。我做错了什么导致我无法存储我的cookie,然后检索它。

AJAX POST

function(config) {
    var config_copy = JSON.parse(JSON.stringify(config));

    //post cookie
    $.ajax({
           url: "php/pivot_cookie.php",
           type: "POST",
           data: config_copy,
           success: function(){ alert("Cookie: success") },
           error: function(data){alert(data);}
     });

}

PHP

$cookie_name = "pivot_config";
$cookie_value = $_POST['config_copy'];
setcookie($cookie_name, $cookie_value, "/");

//cookie check function
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

AJAX GET 我不知道我在“数据”字段中放入什么来检索cookie,所以我可以将它传递给我的其他函数

 $(document).ready(function(){
    $.ajax({
          url: "php/pivot_cookie.php",
          type: "GET",
          data: (cookie - I dont know how to get it),
          success: function(){ alert("Cookie Retreival: success") },
          error: function(data){alert("Cookie Retrieval: failure");}
    });
}

2 个答案:

答案 0 :(得分:1)

您没有在Cookie上设置正确的到期日期。尝试:

setcookie($cookie_name, $cookie_value, time() + 3600, "/"); // expire in 1 hour

see official docs for setcookie

答案 1 :(得分:1)

除了不给PHP setcookie()它想要的第三个参数 您还需要为$.ajax()&#39; .success()方法设置一个参数。

之后,您可以根据需要.success(data)处理data

注意我传递的JSON数据代替data文件中的ajax-post.html属性。

data: {"config_copy": config_copy}

根据您处理的数据,您只需使用Javascript即可 检索cookie数据。

在网络浏览器的控制台中试试以下内容?在您设置cookie之后 (假设您只设置了一个cookie pivot_config

escape( document.cookie.split('=').pop() ).replace('+', ' ')

另外要注意的是,Cookie的数据量可以存储。

pivot_cookie.php

$cookie_name = "pivot_config";

if ( isset( $_POST['config_copy'])) {
    $cookie_value = $_POST['config_copy'];
    setcookie($cookie_name, $cookie_value, time()+3600, "/");

    //cookie check function
    if(!isset($_COOKIE[$cookie_name])) {
        echo "Cookie named '" . $cookie_name . "' is not set!";
    } else {
        echo "Cookie '" . $cookie_name . "' is set!<br>";
        echo "Value is: " . $_COOKIE[$cookie_name];
    }
}
else {
  if ( isset( $_COOKIE[$cookie_name] ) ) {
    echo $_COOKIE[$cookie_name];
  }
}

Ajax的post.html

<html>
<head>
  <title>getting using ajax</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  <script>
  function x (config) {
    var config_copy = JSON.parse(JSON.stringify(config));

    //post cookie
    $.ajax({
            url: "pivot_cookie.php",
            type: "POST",
            data: {"config_copy": config_copy},
            success: function(data,b,c){
              alert("Cookie Retreival: success");
              $("body").html(data);
            },
            error: function(data){alert(data);}
        });

    }

    window.addEventListener("load", function () {
        x("hi post");
    });
  </script>
</head>
<body>
</body>
</html>

Ajax的get.html

  <html>
  <head>
    <title>getting using ajax</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      $.ajax({
          url: "pivot_cookie.php",
          type: "GET",
          data: "(cookie - I dont know how to get it)",
          success: function(data,b,c){
            alert("Cookie Retreival: success");
            $("body").html(data);
          },
          error: function(data){alert("Cookie Retrieval: failure");}
      });
    });
    </script>
  </head>
  <body>
  </body>
  </html>