【问题标题】:deployd authentification using jquery ajax使用 jquery ajax 部署身份验证
【发布时间】:2013-06-18 06:14:47
【问题描述】:

我已经在我的 debian 7.0.0 64 位中安装了 deployd,我也成功安装了 mongodb,我在已部署的仪表板中创建了一些集合和用户集合,然后使用用户指南如何连接和查询已部署的表,我选择 jquery ajax 登录到从我的本地主机站点部署,登录成功后我尝试获取/发布一些数据,但以某种方式部署返回访问被拒绝。我已经创建了集合名称它的人,然后在 GET、POST、PUT 事件中我编写了这段代码:

cancelUnless(me, "You are not logged in", 401);

然后使用这个 ajax 代码,我尝试登录并发布新的人员数据:

$(document).ready(function(){
/* Create query for username and password for login */
var request = new Object;
request.username = 'myusername';
request.password = 'mypassword';
submitaddress = "http://myipaddress:myport/users/login";
$.ajax({ 
    type: "POST",
    url: submitaddress,
    data: request, 
    cache: false, 
    success: function(data){
        var returndata = eval(data);
        /* After Login success try to post people data */
        if (returndata){
            var request2 = new Object;
            request2.name = 'People Name';
            submitaddress2 = "http://myipaddress:myport/people";
                $.ajax({ 
                    type: "POST",
                    url: submitaddress2,
                    data: request2, 
                    cache: false, 
                    success: function(){
                    }
                })  
            }
        }
    }
});

})

登录过程成功,它返回会话ID和我的用户ID,但登录成功后我尝试发布人员数据它返回“您尚未登录”,谁能帮助我,什么是正确的登录方式使用 jquery 从其他网站(跨域)部署?

【问题讨论】:

    标签: jquery authentication cross-domain deployd


    【解决方案1】:

    终于找到答案了,改用jquery ajax登录deployd,我尝试用另一种方式登录,我在php中使用curl,下面是我的代码:

    $url = 'http://myaddress:myport/users/login';
    $postdata = array('username' => 'myusername', 'password' => 'mypassword'); 
    foreach($postdata as $key => $value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    $result = curl_exec($ch);
    $url = 'http://myaddress:myport/mycollection';
    //POST DATA here
    curl_close($ch);
    

    我所有的数据都贴出来了,没有拒绝访问,在以后的使用中,我们可以在同一个主机上使用jquery ajax和这个curl...

    【讨论】:

      【解决方案2】:

      由于您发送的是跨域请求 (CORS),因此 cookie 不会与 HTTP 请求一起自动发送。为了在 jQuery 中启用此功能,您需要将 xhrFileds 的 withCredentials 属性设置为 true;下面的例子:

      /* Create query for username and password for login */
      var request = {};
      request.username = 'MyUsername';
      request.password = 'MyPassword';
      submitaddress = "http://myaddress:myport/users/login";
      $.ajax({ 
          type: "POST",
          url: submitaddress,
          data: request, 
          cache: false,
           xhrFields: {
               withCredentials: true
           },
          success: function(data){
              console.log(data.id);
              /* After Login success try to post people data */    
              if (data.id){
                  var request2 = {};
                  request2.name = 'People Name';
                  submitaddress2 = "http://myaddress:myport/users/me";
                  $.ajax({ 
                      type: "GET",
                      url: submitaddress2,
                      cache: true,
                      xhrFields: {
                          withCredentials: true
                      },
                      success: function(data){
                          console.log(data);
                      }
                  });  
      
              }
          }
      });
      

      更多详情请见jQuery.ajax()

      【讨论】:

      • 谢谢!你刚刚结束了 12 个小时的大脑麻木和故障排除折磨,而我一直在努力解决这个问题。我已经向 Deployd docs 提交了一个拉取请求,以使用 jQuery 更好地记录 CORS。
      猜你喜欢
      • 2012-04-30
      • 2016-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多