2014-01-14 46 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

你怎么知道 –