Reputation: 21
I have installed deployd in my debian 7.0.0 64 bit, I have also succesfully installed mongodb in it, I have create some collection and user collection in deployd dashboard, then using user guide how to connect and query the table in deployd, I choose jquery ajax to log in to deployd from my localhost site and after login success I try to get/post some data, but somehow deployd return access denied. I have create collection name it people, and then at the GET, POST, PUT Event I have write this code :
cancelUnless(me, "You are not logged in", 401);
then using this ajax code, I try to login and POST new people data:
$(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(){
}
})
}
}
}
});
})
The login process success, it's return session id and my user id, but after login success and I try to POST people data it's return "You are not logged in", can anyone help me, what is the correct way to login to deployd using jquery from other website(cross domain)?
Upvotes: 2
Views: 1061
Reputation: 323
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.
Upvotes: 1
Reputation: 21
Finally I found the answer, instead using jquery ajax to login to deployd, I try to use another way to log in, I'm using curl in php, here is my code:
$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);
All my data posted, no access denied, in later use, we can use jquery ajax along with this curl in the same host...
Upvotes: 0