iT邦幫忙

0

JQuery 利用$.ajax 登入.沒法更新SESSION?

JQuery 利用$.ajax 登入.沒法更新SESSION?

我在前端使用者IE頁面寫了一段程式碼.
利用以下程式碼去改寫$_SESSION['mem_email'].判斷使用者是否登入??
難道真的沒辦法.一定要submit網頁嗎??

前端Javavscript
//會員登入**********************************************************
function check_login() {
//前端判斷
	if ($('#mem_email').val()==''){

		$('#msg_label').html("請輸入email");
		return false; 
	}
	if ($('#mem_password').val()==''){

		$('#msg_label').html("請輸入密碼");
		return false; 
	}

//後端檢查
	  $.ajax({
	 url: 'server/server_member_login.php',
	 cache: false,
	 dataType: 'html',
		 type:'GET',
	 data: {  mem_email: $('#mem_email').val(),mem_password: $('#mem_password').val() },
	 error: function(xhr) {
	   alert('Ajax request 發生錯誤');
	 },
	 success: function(response) {
		success_login(response);
	 }
 }); 
 
}

function success_login(response) {

    $('#msg_label').html(response);
    $('#msg_label').fadeIn();
	if (response.trim()=='err'){
		$('#msg_label').html("帳號:"+$('#mem_email').val()+" 不存在,或者密碼有問題!!");
		return false; 
	}
	//alert(response.trim().length);
	//javascript:login.submit();
	
	//後續動作
	response="帳號"+$('#mem_email').val()+"已經登入了";
    $('#mid_lognftext').html(response);
	//
	response='<a href="index.php"><img src="images/howTounit_06.png" width="142" height="31" border="0" /></a>';
	$('#content_info_btn').html(response);	//清空按鈕
    //$('#msg_label').fadeIn();	
	
	
}



	
Server程式碼
$_SESSION['mem_email'] = $_GET["mem_email"];
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

10
fillano
iT邦超人 1 級 ‧ 2010-09-02 15:24:51
最佳解答

如果沒辦法,那有也許是瀏覽器的policy問題。要克服這個問題,你需要在送出request時加上header來讓session id與你頁面一致。例如:

<pre class="c" name="code">
function check_login() {
//省略
	  $.ajax({
	 beforeSend: function(req){
	   req.setRequestHeader("Cookie",document.cookie);
	 },
//省略
 }); 
//省略

你試試看,我是看jQuery的手冊改的,沒有測試過,所以你還是自己測試看看。

詳情請見jquery手冊:
http://api.jquery.com/jQuery.ajax/

我要發表回答

立即登入回答