2

我已经在我的 debian 7.0.0 64 位中安装了部署,我也成功安装了 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?

4

2 回答 2

1

Because you are sending a cross domain request (CORS), the cookies are not automatically sent along with the HTTP request. In order to do enable this in jQuery, you need to set the withCredentials property of xhrFileds to true; example below:

/* 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);
                }
            });  

        }
    }
});

See jQuery.ajax() for more details.

于 2013-08-09T11:01:38.500 回答
0

最后我找到了答案,而不是使用 jquery ajax 登录到已部署,我尝试使用另一种方式登录,我在 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...

于 2013-06-28T04:17:21.990 回答