Ajax和会话不起作用

I need to post something in ajax , and save it in sessions

This is the post code :

$(document).ready(function(){
$(".q").on('change', function postinput(){
    var q = $(this).val(); // this.value
    var id = $(this).attr("id"); // this.id
    $.ajax({ 
        url: 'api/save?id='+id+'&q='+q,
        type: 'get'
    }).done(function(responseData) {
        console.log('Done: ', responseData);
    }).fail(function() {
        console.log('Failed');
    });
});
}); 

And the api code :

session_start()
$id=$_GET['id'];
$_SESSION[$id] = $_GET['q'];
echo 'success';

I used var_dump($_SESSION); to show all sessions

I enter api/save?id=8&q=4 , it print:

array(2) { [""]=> NULL [8]=> string(1) "4" }

But if I enter api/save , it print :

array(1) { [""]=> NULL }
查看全部
dtv8189
dtv8189
2016/08/06 15:05
  • session
  • php
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复