1

I am working on phonegap app and using drupal 7 as backend for all json request data. I am using the service module for it. At frontend I am using Angular js in phonegap.

I referred http:// drupal.stackexchange.com/questions/22277/creating-comments-and-nodes-with-services-3-x-and-json for posting comment.

I set permission for anonymous user to post comment to any node from drupal permission section. But its not posting any comment. Giving me error.

/appapi/comment.json 403: Access denied for user anonymous

Here is my code:

var dataObj = {"subject":$comment.text, "comment_body":{"und":[{"value":$comment.text}]}, "nid":$comment.nid} ; 

var res = $http.post($scope.apiBaseUrl+'/appapi/comment.json', dataObj,{ withCredentials: false, headers: { 'Content-Type': 'application/json'} }); res.success(function(data, status, headers, config) 
{ //$scope.message = data; 
console.log(data); 
 });

Here is CROS module setting:

*|*|POST,GET,PUT,DELETE|Origin, X-Requested-With, Content-Type, Accept|false

I am sending comment post request from phonegap app. I am not using the AgularJS module.

4
  • Please share the JSON data you are POSTing. Also make sure that anonymous users are already to post comments via user permissions. Commented Oct 20, 2015 at 13:52
  • Here is my code var dataObj = {"subject":$comment.text, "comment_body":{"und":[{"value":$comment.text}]}, "nid":$comment.nid} ; var res = $http.post($scope.apiBaseUrl+'/appapi/comment.json', dataObj,{ withCredentials: false, headers: { 'Content-Type': 'application/json'} }); res.success(function(data, status, headers, config) { //$scope.message = data; console.log(data); $ionicLoading.hide(); }); I have already set permission for anonymous user to post comment via permissions.
    – Bhushan
    Commented Oct 20, 2015 at 19:20
  • I have CROS module and its settings are ..*|*|POST,GET,PUT,DELETE|Origin, X-Requested-With, Content-Type, Accept|false Is it something to do with this.
    – Bhushan
    Commented Oct 20, 2015 at 19:25
  • Please update your question to include the additional data, it is hard to decipher it inside a comment. FYI, you may want to check out the angular-drupal module, then all you have to do is call comment_save();: github.com/easystreet3/angular-drupal Commented Oct 20, 2015 at 20:17

1 Answer 1

0

Thanks tyler.frankenstein. Thanks for suggesting angular-drupal. I integrated it with my agularjs ui and its working superb.

I gone through angular-drupal code and found that it need X-CSRF-Token in header for write operation. Accordingly i modified my CORS setting

*|*|POST,GET,PUT,DELETE|Origin, X-Requested-With, X-CSRF-Token, Content-Type, Accept|false

And used agular-drupal method to save comment. Here is my code.

//register drupal service as provider/injector
.factory('$drupal',['drupal', function(drupal) {
  return drupal;
}])
.
.
.
//inside controller

.controller('HomeCtrl', function($scope, $stateParams,$http,$state,$drupal) {


  $drupal.node_load(27).then(function(node) {
    console.log(node.title);
    alert(node.title);
  });

  var comment = {
  nid: 27,
  subject: 'Hello world',
  comment_body: { und: [ { value: 'How are you?' } ] }
};
$drupal.comment_save(comment).then(function(data) {
    alert('Created comment: ' + data.cid);
});

Other way to achieve this without angular-drupal is to generate x-csrf-token using get request "/?q=services/session/token" and then set it in header for your post request.

Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.