Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery ajax部署身份验证_Jquery_Authentication_Cross Domain_Deployd - Fatal编程技术网

使用jquery ajax部署身份验证

使用jquery ajax部署身份验证,jquery,authentication,cross-domain,deployd,Jquery,Authentication,Cross Domain,Deployd,我已经在我的debian 7.0.0 64位中安装了deployd,我也成功地在其中安装了mongodb,我在deployd dashboard中创建了一些集合和用户集合,然后使用user guide如何在deployd中连接和查询表,我选择jqueryajax从本地主机站点登录部署,登录成功后,我尝试获取/发布一些数据,但不知何故部署的返回访问被拒绝。我创建了名为it人员的集合,然后在GET、POST、PUT事件中我编写了以下代码: cancelUnless(me, "You are not

我已经在我的debian 7.0.0 64位中安装了deployd,我也成功地在其中安装了mongodb,我在deployd dashboard中创建了一些集合和用户集合,然后使用user guide如何在deployd中连接和查询表,我选择jqueryajax从本地主机站点登录部署,登录成功后,我尝试获取/发布一些数据,但不知何故部署的返回访问被拒绝。我创建了名为it人员的集合,然后在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 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

因为您正在发送跨域请求(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);
                }
            });  

        }
    }
});

有关详细信息,请参阅。

谢谢!你刚刚结束了12个小时的大脑麻木和折磨,而我一直在努力解决这个问题。我已经向部署的文档提交了一个pull请求,以便使用jQuery更好地编制CORS。