I just set up services 3 on my site, and I created a simple javascript file to test the node resource.
I set up a service using the administration page.
My service export code looks like this:
$endpoint = new stdClass;
$endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */
$endpoint->api_version = 3;
$endpoint->name = 'node_service';
$endpoint->title = 'Node Service';
$endpoint->server = 'rest_server';
$endpoint->path = 'node-service';
$endpoint->authentication = array();
$endpoint->resources = array(
'node' => array(
'operations' => array(
'retrieve' => array(
'enabled' => 1,
),
'create' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 1,
),
'delete' => array(
'enabled' => 1,
),
'index' => array(
'enabled' => 1,
),
),
'relationships' => array(
'files' => array(
'enabled' => 1,
),
'comments' => array(
'enabled' => 1,
),
),
),
);
$endpoint->debug = 0;
My html/javascript file looks like this:
<html>
<head>
<script src="misc/jquery.js" type="text/javascript"></script>
</head>
<body>
<p>View node:</p>
<form name="view_node_form" method="post" onsubmit="javascript:return viewNode();">
Node ID: <input id="node_id" type="text" name="nid" /><br>
<input type="submit" value="Submit" />
</form>
<p>Create node:</p>
<form name="create_node_form" method="post" onsubmit="javascript:return createNode();">
Node Title: <input id="node_title" type="text" name="nid" /><br>
<input type="submit" value="Submit" />
</form>
<div id="data"></div>
<script type="text/javascript">
function f(data){
var output='';
output+='<h2>'+data.title+'</h2>';
output+=data.body.und[0].value;
output+=JSON.stringify(data);
$('#data').html(output);
}
function displayCreated(data){
$('#data').html(data);
}
function viewNode(){
var nid=$('#node_id').val()
$.ajax({
type:"GET",
url:"/node-service/node/"+nid,
dataType:'json',
success:f
});
return false;
}
function createNode(){
var data={};
data['title']=$('#node_title').val();
data['type']='page';
$.ajax({
type: "POST",
url: "node-service/node",
data:data,
dataType: 'json',
contentType: 'application/json',
success:displayCreated
});
return false;
}
</script>
</body>
</html>
The retrieve form works as expected. But the create form always causes the authentication popup to appear, where it asks for a user name and password.
In the same browser I have logged in as User 1. I installed node access, so that I could block off certain types of nodes from being viewed. If I logout, the retrieve is no longer able to get restricted nodes. If I'm logged in as user one the retreive form can get all nodes. So I know that the authentication is working for the retrieve form, but for some reason it never thinks I'm authenticated when using the create form.
Is there something I'm doing wrong in the javascript that could cause this? Maybe I'm just passing the data incorrectly to the service.
Comments
Comment #1
kleinmp commentedYou probably need to check that you're using 'Session based Authentication'
I was having the same issue. I found that that at the top of services_controller_execute, the user is set to the anonymous user, then the authentication methods are called, and then later the access callback for the node creation is called.
So, if you haven't checked that you're using session based authentication via Drupal, then this won't work, because it forces users to be anonymous.
Comment #2
kleinmp commentedI spoke too soon and edited my reply in comment #1. I'm changing this back to support request.
Comment #3
kylebrowning commented