我已经在我的debian 7.0.0 64位安装了deploy,我也成功地在其中安装了mongodb,我在部署仪表板中创建了一些集合和用户集合,然后使用用户指南如何连接和查询部署中的表,我选择jquery ajax从我的localhost站点登录到部署,登录成功后我尝试获取/发布一些数据,但不知何故部署返回访问拒绝。我已经创建了集合名称它的人,然后在GET, POST, PUT事件我写了以下代码:
cancelUnless(me, "You are not logged in", 401);
然后使用这个ajax代码,我尝试登录和POST新的人的数据:
$(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,但登录成功后,我尝试POST人的数据返回"你没有登录",谁能帮助我,什么是正确的方式登录部署使用jquery从其他网站(跨域)?
因为您正在发送跨域请求(CORS),所以cookie不会自动随HTTP请求一起发送。为了在jQuery中启用此功能,您需要将xhrfields的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登录部署,我尝试使用另一种方式登录,我在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在同一主机…