Answers.Body: <p>If you aren't currently logged in, you're apt to be running up against the "anonymous access" limitation.</p>
  
  <p><a href="https://craftcms.com/docs/plugins/controllers#allowing-anonymous-access-to-actions" rel="nofollow noreferrer">This section of the Controller documentation</a> should help:</p>
  
  <blockquote>
    <p>By default, controller actions are only accessible to logged-in users. You can override that by changing the protected <code>$allowAnonymous</code> property on your controller class. To allow anonymous access to all of your controller’s actions, set it to <code>true</code>:</p>
  </blockquote>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  namespace Craft;
  
  class CocktailRecipes_IngredientsController extends BaseController
  {
    protected $allowAnonymous = true;
  
    // Your action methods
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-31T23:30:10.740Z
Answers.Text: Hi thank you... I tried that it doesnt work
Answers.UserDisplayName: MariaZ
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-31T21:47:18.573Z
Answers.LastActivityDate: 2017-07-31T21:47:18.573Z
Answers.UserDisplayName: August Miller
Answers.Body: <p>Two things to try:</p>
  
  <p>1) Try to use the Craft.getActionUrl() to get the URL of the action, as per the documentation for <a href="https://craftcms.com/docs/plugins/controllers#linking-directly-to-controller-actions" rel="nofollow noreferrer">Linking Directly to Controller Actions</a>:</p>
  
  <p>2) Make sure there is no <code>$this-&gt;requireLogin()</code> or <code>$this-&gt;requirePostRequest()</code> in your controller</p>
  
  <p>Theoretically there could be something wrong in the routing as well, as I have also experienced that a plugin runs perfectly, but because the response was not set to reply or re-route to somewhere meaningful, I got a 404 when accessing the controller via URL, and not from the control panel. In that case, this answered question could be helpful: <a href="https://craftcms.stackexchange.com/questions/5401/controller-action-works-but-still-gives-404">Controller action works but still gives 404</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-02T01:18:01.923Z
Answers.Text: Thaank you!!... that links sounds interesting... I will have a look
Answers.UserDisplayName: MariaZ
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-01T13:34:06.327Z
Answers.LastActivityDate: 2017-08-01T13:34:06.327Z
Answers.UserDisplayName: KSP
Answers.Body: <p>If you are still not having any luck, If none of the above work, <a href="https://craftcms.stackexchange.com/questions/12736/how-can-i-call-a-controller-method-from-the-front-end-using-ajax">check out this thread</a> that shows how to call an ajax function from a controller.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-02T01:18:37.547Z
Answers.Text: I have seen that thread I made it look the same and nothing... but thaaanks!
Answers.UserDisplayName: MariaZ
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-02T04:02:56.447Z
Answers.Text: Try making your ajax request path start with a `/`. Example: `/actions/pluginHandle/checkStatus`
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-01T13:40:20.047Z
Answers.LastActivityDate: 2017-08-01T13:40:20.047Z
Answers.UserDisplayName: Damon
Body: <p>I have created a small craft cms plugin to pull some information from the DB, and everytime I execute the plugin I get a 404.   </p>
  
  <p>Here is the controller</p>
  
  <pre><code>namespace Craft;
  
  class UserExists_UserControllerController extends BaseController
  {
      public function actionGetUser()
      {
          $this-&gt;returnJson($response);
      }
  }
  </code></pre>
  
  <p>And this is the jquery:</p>
  
  <pre><code>$.ajax({
      url: "{{ url("actions/userexists/usercontroller/getUser") }}",
      dataType: "json",
      success: function(data){
          console.log(data)
      }
  });
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-31T21:49:55.887Z
Comments.Text: I've answered below, but wanted to offer one other (unrelated) recommendation: Your class's name can be shortened to `UserExists_UserController`—I suggest, only because the use or omission of the word "controller" in actions may become confusing! Using this class name will allow your AJAX call to be made to `actions/userexists/user/getUser`, instead.
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-31T21:53:19.117Z
Comments.Text: Another possibility is to use a top-level controller—if your plugin isn't apt to have many other controllers or actions, you could simplify the naming even further: `UserExists_Controller`, and access it via `actions/userexists/getUser`.
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-31T21:53:41.257Z
Comments.Text: I'm concerned, though, that the 404 error you're getting may have something to do with the naming of the file and class. Could you pots the filename that you've defined `UserExists_UserControllerController` in?
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-31T23:28:59.710Z
Comments.Text: Hi! thanks so much for your reply. I have done that i keep getting 404
Comments.UserDisplayName: MariaZ
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-31T23:32:24.803Z
Comments.Text: Do you mean UserExistsPlugin.php ?
Comments.UserDisplayName: MariaZ
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-01T16:58:32.787Z
Comments.Text: That is your base plugin definition file. Your plugin folder should also have a `controllers` directory, containing `UserExists_UsersController.php`, which defines the class `UserExists_UsersController`. Action methods in that class will be available at `actions/userexists/users/getUser`.
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-01T16:59:24.550Z
Comments.Text: Take a look at https://pluginfactory.io if you're having trouble with the overall plugin structure—it'll mock out a clean starting point for you.
Comments.UserDisplayName: August Miller
Tags: <plugin-development><ajax><controller>
Title: Craft CMS plugin with AJAX gives 404 error
ThreadText: # Craft CMS plugin with AJAX gives 404 error
  
  (Asked by: username_0 on 2017-07-31)
  
  I have created a small craft cms plugin to pull some information from the DB, and everytime I execute the plugin I get a 404.
  
  Here is the controller
  
  ```
  namespace Craft;
  
  class UserExists_UserControllerController extends BaseController
  {
      public function actionGetUser()
      {
          $this->returnJson($response);
      }
  }
  
  ```
  
  And this is the jquery:
  
  ```
  $.ajax({
      url: "{{ url("actions/userexists/usercontroller/getUser") }}",
      dataType: "json",
      success: function(data){
          console.log(data)
      }
  });
  
  ```
  
  **Tags:** plugin-development, ajax, controller
  
  > **Comments:**
  > **username_1**: I've answered below, but wanted to offer one other (unrelated) recommendation: Your class's name can be shortened to `UserExists_UserController`—I suggest, only because the use or omission of the word "controller" in actions may become confusing! Using this class name will allow your AJAX call to be made to `actions/userexists/user/getUser`, instead.  
  > **username_1**: Another possibility is to use a top-level controller—if your plugin isn't apt to have many other controllers or actions, you could simplify the naming even further: `UserExists_Controller`, and access it via `actions/userexists/getUser`.  
  > **username_1**: I'm concerned, though, that the 404 error you're getting may have something to do with the naming of the file and class. Could you pots the filename that you've defined `UserExists_UserControllerController` in?  
  > **username_0**: Hi! thanks so much for your reply. I have done that i keep getting 404  
  > **username_0**: Do you mean UserExistsPlugin.php ?  
  > **username_1**: That is your base plugin definition file. Your plugin folder should also have a `controllers` directory, containing `UserExists_UsersController.php`, which defines the class `UserExists_UsersController`. Action methods in that class will be available at `actions/userexists/users/getUser`.  
  > **username_1**: Take a look at https://pluginfactory.io if you're having trouble with the overall plugin structure—it'll mock out a clean starting point for you.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2017-07-31. Score: 4
  
  If you aren't currently logged in, you're apt to be running up against the "anonymous access" limitation.
  
  [This section of the Controller documentation](https://craftcms.com/docs/plugins/controllers#allowing-anonymous-access-to-actions) should help:
  
  > By default, controller actions are only accessible to logged-in users. You can override that by changing the protected `$allowAnonymous` property on your controller class. To allow anonymous access to all of your controller’s actions, set it to `true`:
  
  ```php
  <?php
  namespace Craft;
  
  class CocktailRecipes_IngredientsController extends BaseController
  {
    protected $allowAnonymous = true;
  
    // Your action methods
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Hi thank you... I tried that it doesnt work  
  
  ---
  
  ### Answer by username_2 on 2017-08-01. Score: 2
  
  Two things to try:
  
  1) Try to use the Craft.getActionUrl() to get the URL of the action, as per the documentation for [Linking Directly to Controller Actions](https://craftcms.com/docs/plugins/controllers#linking-directly-to-controller-actions):
  
  2) Make sure there is no `$this->requireLogin()` or `$this->requirePostRequest()` in your controller
  
  Theoretically there could be something wrong in the routing as well, as I have also experienced that a plugin runs perfectly, but because the response was not set to reply or re-route to somewhere meaningful, I got a 404 when accessing the controller via URL, and not from the control panel. In that case, this answered question could be helpful: [Controller action works but still gives 404](https://craftcms.stackexchange.com/questions/5401/controller-action-works-but-still-gives-404).
  
  > **Comments:**
  > **username_0**: Thaank you!!... that links sounds interesting... I will have a look  
  
  ---
  
  ### Answer by username_3 on 2017-08-01. Score: 1
  
  If you are still not having any luck, If none of the above work, [check out this thread](https://craftcms.stackexchange.com/questions/12736/how-can-i-call-a-controller-method-from-the-front-end-using-ajax) that shows how to call an ajax function from a controller.
  
  > **Comments:**
  > **username_0**: I have seen that thread I made it look the same and nothing... but thaaanks!  
  > **username_3**: Try making your ajax request path start with a `/`. Example: `/actions/pluginHandle/checkStatus`  
  
  ---
  
  