Answers.Body: <p>Turns out the <a href="https://www.npmjs.com/package/xhr" rel="nofollow">ajax library</a> I was using was expecting the data to be passed in a serialized format:</p>
  
  <p><code>'comment=' + comment + '&amp;entryId=' + entryId + '&amp;authorId=' + authorId</code></p>
  
  <p>With that as the body of the request, the data became accessible to my controlled by using <code>craft()-&gt;request-&gt;getPost</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-04T19:12:05.807Z
Answers.LastActivityDate: 2016-05-04T19:12:05.807Z
Answers.UserDisplayName: Trevor Davis
Body: <p>I'm writing a plugin to notify authors when a Disqus comment is posted. There is a Disqus JS callback you can use to hook into this, so I'm doing the following on the front-end:</p>
  
  <pre><code>this.callbacks.onNewComment = [function(comment) {
    var commentNotification = new window.VIGET.CommentNotification();
  
    commentNotification.notify('{{ actionUrl("vigetDisqus/notify/notify") }}', comment, {{ entry.id }}, {{ entry.author.id }});
  }];
  </code></pre>
  
  <p>So I am passing the actionUrl of my controller, the comment itself, the entry id, and the author id. This all works perfectly fine, and I am passing it as data along as an AJAX request to the controller.</p>
  
  <pre><code>CommentNotification.prototype = {
    notify: function(actionUrl, comment, entryId, authorId) {
      var data = JSON.stringify({
        comment: comment,
        entryId: entryId,
        authorId: authorId
      });
  
      xhr({
        uri: actionUrl,
        method: 'POST',
        body: data,
        headers: {
          'Content-Type': 'application/json',
          'X-Requested-With': 'XMLHttpRequest'
        }
      }, function(err, resp) {
        console.log(resp.body)
      });
    }
  };
  </code></pre>
  
  <p>How do I access the posted data inside my controller? I thought I could use the getPost method, but I am always getting null.</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  class VigetDisqus_NotifyController extends BaseController
  {
      protected $allowAnonymous = true;
  
      public function actionNotify()
      {
          $this-&gt;requirePostRequest();
          $this-&gt;requireAjaxRequest();
  
          $authorId = craft()-&gt;request-&gt;getPost('authorId');
  
          $this-&gt;returnJson(array('authorId' =&gt; $authorId));
      }
  }
  </code></pre>
  
Tags: <plugin-development><ajax><controller><frontend>
Title: Access data when posting to controller from front-end
ThreadText: # Access data when posting to controller from front-end
  
  (Asked by: username_0 on 2016-05-04)
  
  I'm writing a plugin to notify authors when a Disqus comment is posted. There is a Disqus JS callback you can use to hook into this, so I'm doing the following on the front-end:
  
  ```
  this.callbacks.onNewComment = [function(comment) {
    var commentNotification = new window.VIGET.CommentNotification();
  
    commentNotification.notify('{{ actionUrl("vigetDisqus/notify/notify") }}', comment, {{ entry.id }}, {{ entry.author.id }});
  }];
  
  ```
  
  So I am passing the actionUrl of my controller, the comment itself, the entry id, and the author id. This all works perfectly fine, and I am passing it as data along as an AJAX request to the controller.
  
  ```
  CommentNotification.prototype = {
    notify: function(actionUrl, comment, entryId, authorId) {
      var data = JSON.stringify({
        comment: comment,
        entryId: entryId,
        authorId: authorId
      });
  
      xhr({
        uri: actionUrl,
        method: 'POST',
        body: data,
        headers: {
          'Content-Type': 'application/json',
          'X-Requested-With': 'XMLHttpRequest'
        }
      }, function(err, resp) {
        console.log(resp.body)
      });
    }
  };
  
  ```
  
  How do I access the posted data inside my controller? I thought I could use the getPost method, but I am always getting null.
  
  ```
  <?php
  namespace Craft;
  
  class VigetDisqus_NotifyController extends BaseController
  {
      protected $allowAnonymous = true;
  
      public function actionNotify()
      {
          $this->requirePostRequest();
          $this->requireAjaxRequest();
  
          $authorId = craft()->request->getPost('authorId');
  
          $this->returnJson(array('authorId' => $authorId));
      }
  }
  
  ```
  
  **Tags:** plugin-development, ajax, controller, frontend
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-05-04. Score: 2
  
  Turns out the [ajax library](https://www.npmjs.com/package/xhr) I was using was expecting the data to be passed in a serialized format:
  
  `'comment=' + comment + '&entryId=' + entryId + '&authorId=' + authorId`
  
  With that as the body of the request, the data became accessible to my controlled by using `craft()->request->getPost`
  
  
  ---
  
  