<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>

CC BY-SA 3.0
2017-07-31T23:30:10.740Z
Hi thank you... I tried that it doesnt work
MariaZ
CC BY-SA 3.0
2017-07-31T21:47:18.573Z
2017-07-31T21:47:18.573Z
August Miller
<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>

CC BY-SA 3.0
2017-08-02T01:18:01.923Z
Thaank you!!... that links sounds interesting... I will have a look
MariaZ
CC BY-SA 3.0
2017-08-01T13:34:06.327Z
2017-08-01T13:34:06.327Z
KSP
<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>

CC BY-SA 3.0
2017-08-02T01:18:37.547Z
I have seen that thread I made it look the same and nothing... but thaaanks!
MariaZ
CC BY-SA 3.0
2017-08-02T04:02:56.447Z
Try making your ajax request path start with a `/`. Example: `/actions/pluginHandle/checkStatus`
Damon
CC BY-SA 3.0
2017-08-01T13:40:20.047Z
2017-08-01T13:40:20.047Z
Damon
<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>

CC BY-SA 3.0
2017-07-31T21:49:55.887Z
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.
August Miller
CC BY-SA 3.0
2017-07-31T21:53:19.117Z
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`.
August Miller
CC BY-SA 3.0
2017-07-31T21:53:41.257Z
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?
August Miller
CC BY-SA 3.0
2017-07-31T23:28:59.710Z
Hi! thanks so much for your reply. I have done that i keep getting 404
MariaZ
CC BY-SA 3.0
2017-07-31T23:32:24.803Z
Do you mean UserExistsPlugin.php ?
MariaZ
CC BY-SA 3.0
2017-08-01T16:58:32.787Z
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`.
August Miller
CC BY-SA 3.0
2017-08-01T16:59:24.550Z
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.
August Miller
<plugin-development><ajax><controller>
Craft CMS plugin with AJAX gives 404 error
# 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 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:

> **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`  

---

