Answers.Body: <p>Make sure to check out <a href="https://craftcms.com/docs/routing" rel="nofollow">the documentation</a> on routing. Pay special attention to the part about action request(s).</p>
  
  <p>In this example, I want to call a controller method via ajax on page load.</p>
  
  <p><strong>MyPluginController.php</strong></p>
  
  <pre><code>...
  
  /**
   * Checks what the current status is RIGHT. NOW.
   *
   * @throws HttpException
   *
   */
  public function actionCheckStatus()
  {
      $this-&gt;requireAjaxRequest();
  
      $response = [
          'status' =&gt; 'open'
      ];
  
      $this-&gt;returnJson($response);
  }
  
  ...
  </code></pre>
  
  <p><strong>jquery</strong></p>
  
  <pre><code>$.ajax({
      'type': 'post',
      'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
      'cache': false,
      'data': data,
      'url': '/actions/pluginHandle/checkStatus',
      'dataType': 'json',
      'timeout': 50000
  }).done(function (response) {
      // Success
  }).fail(function (error) {
     // Total fail.
  });
  </code></pre>
  
  <p>The key part to note is the url attribute in the ajax request. It is prepended with 'actions'. As you will note in the docs you can change 'actions' to whatever you want.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-06T00:55:38.640Z
Answers.LastActivityDate: 2016-01-17T17:18:33.287Z
Answers.LastEditDate: 2016-01-17T17:18:33.287Z
Answers.UserDisplayName: Damon
Answers.Body: <p>For Craft 3:</p>
  
  <pre><code>public function actionCheckStatus()
  {
      $this-&gt;requireAcceptsJson();
  
      $response = [
          'status' =&gt; 'open'
      ];
  
      return $this-&gt;asJson($response);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-01T11:45:13.230Z
Answers.LastActivityDate: 2018-06-01T20:02:01.957Z
Answers.LastEditDate: 2018-06-01T20:02:01.957Z
Answers.UserDisplayName: Jeroen
Body: <p>I would like to call an action method from within my plugin via ajax but I am always getting a 404 response. </p>
  
  <p>I have a controller method that matches the ajax url:</p>
  
  <p><strong>jquery</strong></p>
  
  <pre><code>$.ajax({
      'type': 'post',
      'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
      'cache': false,
      'data': data,
      'url': 'pluginHandle/checkStatus',
      'dataType': 'json',
      'timeout': 50000
  }).done(function (response) {
      // Success
  }).fail(function (error) {
     // Total fail.
  });
  </code></pre>
  
  <p><strong>MyPluginController.php</strong></p>
  
  <pre><code>...
  
  /**
   * Checks what the current status is RIGHT. NOW.
   *
   * @throws HttpException
   *
   */
  public function actionCheckStatus()
  {
      $this-&gt;requireAjaxRequest();
  
      $response = [
          'status' =&gt; 'open'
      ];
  
      $this-&gt;returnJson($response);
  }
  
  ...
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
Tags: <plugin-development><routes><ajax>
Title: How can I call a controller method from the front-end using ajax
ThreadText: # How can I call a controller method from the front-end using ajax
  
  (Asked by: username_0 on 2015-12-06)
  
  I would like to call an action method from within my plugin via ajax but I am always getting a 404 response.
  
  I have a controller method that matches the ajax url:
  
  **jquery**
  
  ```
  $.ajax({
      'type': 'post',
      'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
      'cache': false,
      'data': data,
      'url': 'pluginHandle/checkStatus',
      'dataType': 'json',
      'timeout': 50000
  }).done(function (response) {
      // Success
  }).fail(function (error) {
     // Total fail.
  });
  
  ```
  
  **MyPluginController.php**
  
  ```
  ...
  
  /**
   * Checks what the current status is RIGHT. NOW.
   *
   * @throws HttpException
   *
   */
  public function actionCheckStatus()
  {
      $this->requireAjaxRequest();
  
      $response = [
          'status' => 'open'
      ];
  
      $this->returnJson($response);
  }
  
  ...
  
  ```
  
  What am I doing wrong?
  
  **Tags:** plugin-development, routes, ajax
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-12-06. Score: 4 | Accepted answer
  
  Make sure to check out [the documentation](https://craftcms.com/docs/routing) on routing. Pay special attention to the part about action request(s).
  
  In this example, I want to call a controller method via ajax on page load.
  
  **MyPluginController.php**
  
  ```
  ...
  
  /**
   * Checks what the current status is RIGHT. NOW.
   *
   * @throws HttpException
   *
   */
  public function actionCheckStatus()
  {
      $this->requireAjaxRequest();
  
      $response = [
          'status' => 'open'
      ];
  
      $this->returnJson($response);
  }
  
  ...
  
  ```
  
  **jquery**
  
  ```
  $.ajax({
      'type': 'post',
      'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
      'cache': false,
      'data': data,
      'url': '/actions/pluginHandle/checkStatus',
      'dataType': 'json',
      'timeout': 50000
  }).done(function (response) {
      // Success
  }).fail(function (error) {
     // Total fail.
  });
  
  ```
  
  The key part to note is the url attribute in the ajax request. It is prepended with 'actions'. As you will note in the docs you can change 'actions' to whatever you want.
  
  
  ---
  
  ### Answer by username_1 on 2018-06-01. Score: 2
  
  For Craft 3:
  
  ```
  public function actionCheckStatus()
  {
      $this->requireAcceptsJson();
  
      $response = [
          'status' => 'open'
      ];
  
      return $this->asJson($response);
  }
  
  ```
  
  
  ---
  
  