2014-01-14 52 views
0

我試圖建立一個用戶必須登錄後的評論api。 伊夫隱藏評論按鈕,將其設置在用戶登錄後顯示。我怎樣才能通過用戶名登錄

var loggeduser; 
    function Login(){ 
    $.ajax({ 
     url: 'http://creative.coventry.ac.uk/~4089797/Games/V2.0/Client4/index.php/users/account/'+$("#loginusername").val(), 
    headers: {Authorization:window.btoa($("#loginusername").val()+':'+$("#loginpassword").val())}, 
    contentType: "get", 
     dataType: 'json', 
     success: function(data) { 
      Authorisation = window.btoa($("#loginusername").val()+':'+$("#loginpassword").val()); //create a variable to pass to the cookie 
      createCookie('auth',Authorisation,0); //create our authorisation cookie for the client 
      alert("Details correct. Please continue."); 
      loggeduser=username; 
      $("#login").hide(); //hide the login button 
      $("#create").hide(); //hide the create an account buttons 
      $("#logout").show(); //show the logout button 
      $("#addreview").show(); //show the add a review button 
      $("#adminpanel").show();//show the admin panel page 
      $("#loginusername").val(''); //clear the name box 
      $.mobile.changePage("#home"); //show the menu 

     }, 
    error: function (response) { 
     var r = jQuery.parseJSON(response.responseText); 
     console.log(r); 
     alert("Error:" + r.error.text); 
        } 
      }); 
} 

香港專業教育學院使用上述Ajax調用登錄的用戶,並試圖通過此用戶名去loggeduser。

function Postreview(){ //Add a review to the site 
    $.ajax({ 
    type:'post', 
     url: 'http://creative.coventry.ac.uk/~4089797/Games/V2.0/Client4/index.php/games/review/', 
    data: {ean:reviewean, 
    review_title:$("#ReviewTitle").val(), 
    review:$("#Review").val(), 
    rating:$("#rating").val(), 
    username:loggeduser}, 
    dataType: 'json', 
     success: function(data) { 
      alert("Review Added. Please continue."); 
      $("#reviewtitle").val(''); //clear the text boxes 
      $("#review").val(''); 
      $("#rating").val(''); 
      Getreviews(ean); 


     }, 
    error: function (response) { 
     var r = jQuery.parseJSON(response.responseText); 
     console.log(r); 
     alert("Error:" + r.error.text); 
        } 
      }); 
} 

這是用戶帖子的審查然而,當用戶提交審查的用戶名字段爲空,因此處理不當對

回答

0

在登錄Ajax響應被通過,loggeduser設置爲username

loggeduser=username;

...但是username似乎不存在?也許你的意思是data.username

無論如何,您應該從服務器返回已通過身份驗證的用戶名,並使用data.username來設置loggeduser

此外,將密碼存儲在cookie中是一個糟糕的主意。你在使用PHP會話嗎?

+0

即時通訊不知道什麼是一個PHP會話?並且密碼將被存儲爲散列 – user3187726

+0

對PHP會話進行一些研究。他們處理這種情況,所以您不必通過每個請求來驗證用戶身份。而base-64不是一個散列。它可以用'atob()'解碼。 – bearfriend

+0

也只是數據。 LIFE保護感謝 – user3187726

-1

這不是如何AJAX工作,其中A代表asynchronous

你有兩個選擇

1)在你的Login() AJAX成功做Postreview(用戶名)

2)不建議:異步:假登錄阿賈克斯

+0

這是什麼不是異步? – bearfriend

+0

你怎麼知道 –