2014-08-31 58 views
0

我有两个函数都获得user_id的用户。一个函数获取会话user_id,另一个函数获取URL中的id,如下所示:profile.html?id = 123。这里的代码:我似乎无法做到这一点,如果声明在JavaScript中工作:S

// get URL parameter (user_id) 
     function GetURLParameter(sParam) { 
      var sPageURL = window.location.search.substring(1); 
      var sURLVariables = sPageURL.split('&'); 
      for (var i = 0; i < sURLVariables.length; i++) 
      { 
       var sParameterName = sURLVariables[i].split('='); 
       if (sParameterName[0] == sParam) 
       { 
        return sParameterName[1]; 
       } 
      } 
     } 

     // get URL parameter (user_id) function call 
     url_user_id = GetURLParameter('id'); 
     alert("url user id = " + url_user_id); 

     // get SESSION user_id 
     function get_session_user_id() { 
      $.post("http://localhost/snapll_back/account/session_user_id.php", 
      function(info) {   
       if(info != "Something went wrong. ERROR{1_RAI_ERROR}") { 
        session_user_id = info; 
        alert("session user id = " + session_user_id); 

        // hide the follow button if this me is owned by the viewer 
        if(session_user_id == url_user_id) { 
         $("#follow_button").hide(); 
        } 

        // THE QUESTION ON S.O. IS ABOUT THIS IF STATEMENT 
        // give the url_user_id variable the value of the session_user_id variable if not set correctly via the URL 
        if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) { 
         url_user_id = session_user_id; 
        } 
       } 
       else { 
        $('#warning').html(info).fadeIn(200); 
        setTimeout(function() { 
         window.location.href = "index.html"; 
        }, 2000); 
       } 
      }); 
      return false; 
     } 

     get_session_user_id(); 

所以,我需要做的是检查是否设置变量'url_user_id'。如果不是,则url_user_id应该与变量'session_user_id'获得相同的值。但是,由于某种原因,这不会发生。我在代码中的if语句上面这一部分:

if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) { 
    url_user_id = session_user_id; 
} 

我用url_user_id变量获得当前配置文件的用户正在观看的用户名。下面是获取用户名的功能:

// get username function 
     function get_username() { 
      $.post("http://localhost/snapll_back/account/username.php?id="+url_user_id, 
      function(info) {   
       if(info != "Something went wrong. ERROR{1_RAI_ERROR}") { 
        $("#main_header_username").html(info); 
       } 
       else { 
        $('#warning').html(info).fadeIn(200); 
       } 
      }); 
      return false;  
     } 

当我访问的页面,像这样的网址:www.mywebsite.com/profile.php?id=(所以,如果我指定的URL没有ID)的变量url_user_id(通常从URL获取其值)应该获取变量session_user_id的值。目前,我一直收到'undefined'作为url_user_id的值,if语句似乎没有注意到该值采取行动。

谁知道我该如何解决这个问题?

+ ============================================= ========================================== + EDIT

I现在可以为url_user_id提供正确的值,但是我的函数get_username()无法访问该值。我get_session_user_id函数现在看起来是这样的:

// get SESSION user_id 
     function get_session_user_id() { 
      $.post("http://localhost/snapll_back/account/session_user_id.php", 
      function(info) {   
       if(info != "Something went wrong. ERROR{1_RAI_ERROR}") { 
        session_user_id = info; 
        alert("session user id = " + session_user_id); 

        // if url_user_id is not defined by the URL give it the value of session_user_id 
        if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) { 
         url_user_id = session_user_id; 
         alert("url user id = " + url_user_id); 
        } 

        // hide the follow button if this me is owned by the viewer 
        if(session_user_id == url_user_id) { 
         $("#follow_button").hide(); 
        } 
       } 
       else { 
        $('#warning').html(info).fadeIn(200); 
        setTimeout(function() { 
         window.location.href = "index.html"; 
        }, 2000); 
       } 
      }); 
      return false; 
     } 

它提醒url_user_id与当在URL中没有指定ID正确的值,但get_userame功能仍说“未定义”当我在那个函数提醒它。为什么get_username函数不能获取url_user_id的最新值?

+1

这也是我的第一个猜测,但他正在做一个空软检查,它也应该捕获未定义。 'url_user_id == null' ==='url_user_id == undefined'。 – iabw 2014-08-31 18:15:36

+1

这是可能的[如何从Ajax调用返回响应?](http://stackoverflow.com/q/14220321/1048572)的重复?目前还不清楚你试图访问失败的'url_user_id'。 – Bergi 2014-08-31 18:23:56

+0

我试图在函数get_username()中访问它,您可以在其中看到发布请求中的URL以变量'url_user_id'结尾。在该函数中调用的PHP文件中,有一个GET变量用于获取正确的用户信息。 – 2014-08-31 18:33:20

回答

1

看起来您在get_session_user_id有机会返回之前立即调用get_username。

尝试这样做:

// get URL parameter (user_id) 
    function GetURLParameter(sParam) { 
     var sPageURL = window.location.search.substring(1); 
     var sURLVariables = sPageURL.split('&'); 
     for (var i = 0; i < sURLVariables.length; i++) 
     { 
      var sParameterName = sURLVariables[i].split('='); 
      if (sParameterName[0] == sParam) 
      { 
       return sParameterName[1]; 
      } 
     } 
    } 

    // get URL parameter (user_id) function call 
    url_user_id = GetURLParameter('id'); 

    // get SESSION user_id 
    function get_session_user_id() { 
     $.post("http://localhost/snapll_back/account/session_user_id.php", 
     function(info) {   
      if(info != "Something went wrong. ERROR{1_RAI_ERROR}") { 
       session_user_id = info; 
       //alert("session user id = " + session_user_id); 

       // if url_user_id is not defined by the URL give it the value of session_user_id 
       if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) { 
        url_user_id = session_user_id; 
        //alert("url user id = " + url_user_id); 
       } 

       // hide the follow button if this me is owned by the viewer 
       if(session_user_id == url_user_id) { 
        $("#follow_button").hide(); 
       } 
      } 
      else { 
       $('#warning').html(info).fadeIn(200); 
       setTimeout(function() { 
        window.location.href = "index.html"; 
       }, 2000); 
      } 

      // get username function call 
      get_username(); 

     }); 
     return false; 
    } 

    get_session_user_id(); 

    // get username function 
    function get_username() { 
     //alert(url_user_id); 
     $.post("http://localhost/snapll_back/account/username.php?id="+url_user_id, 
     function(info) {   
      if(info != "Something went wrong. ERROR{1_RAI_ERROR}") { 
       $("#main_header_username").html(info); 
      } 
      else { 
       $('#warning').html(info).fadeIn(200); 
      } 
     }); 
     return false;  
    } 

    /* 
    * function calls 
    */ 
    // check session function call 
    check_session(); 

我们移动成功回调内部调用get_username到您的文章,但在其他的代码是一样的。

+0

Thx,它做到了! – 2014-09-09 14:14:58

相关问题