<p>Based on your controller's class name, I believe you need to POST to this endpoint...</p>

<pre><code>/actions/googleMap/map/getLocations
</code></pre>

CC BY-SA 4.0
2019-03-01T16:19:35.000Z
Hey Lindsey, thank you for the answer. I tried switching to that, but unfortunately I still get the 404 error.
WTFranklin
CC BY-SA 4.0
2019-03-01T17:31:31.837Z
I take that back, I actually get a 400 Bad Request error now.
WTFranklin
CC BY-SA 4.0
2019-03-01T18:53:27.353Z
400 is better than 404, it means the route is correct! It sounds like you're now bumping into [this CSRF issue...](https://craftcms.stackexchange.com/a/6588/45)
Lindsey D
CC BY-SA 4.0
2019-03-02T00:35:50.673Z
Hey you're right, I don't want to go far off topic from this post, so I made another since I'm still having the CSRF issue after checking out your link. This is the link to the post https://craftcms.stackexchange.com/questions/29697/csrf-token-is-null-or-empty-in-craft-2 if you have a minute to check it out :)
WTFranklin
CC BY-SA 4.0
2019-03-02T00:38:51.903Z
Good thinking. We'll continue on the new thread.
Lindsey D
CC BY-SA 4.0
2019-03-01T01:26:37.310Z
2019-03-01T01:26:37.310Z
Lindsey D
<p>I have been stuck on trying to make an ajax call to a custom plugin controller in Craft 2 for a few days after trying everything from following the documentation for Craft 2 as well as looking up other posts that basically reiterate what the documentation says, but nothing seems to work. According to the documentation for Craft 2, I should be able to use:</p>

<pre><code>[PluginHandle]/[ControllerName]/[ActionName]
</code></pre>

<p>in a hidden field. I've even tried using the full method name, actionGetLocations, just to rule that out. Here is what I have:</p>

<pre><code>&lt;input type="hidden" name="action" value="/actions/googleMap/getLocations"&gt;
&lt;input type="hidden" name="redirect" value="{{ siteUrl }}map-test"&gt;
</code></pre>

<p>Here is my controller:</p>

<pre><code>&lt;?php

namespace Craft;

class GoogleMap_MapController extends BaseController
{
    public function actionGetLocations()
    {
        $this-&gt;requireAjaxRequest();

        return $this-&gt;returnJson([
            'success' =&gt; [
                'state' =&gt; "This would be states",
                'locations' =&gt; "This would be locations"
            ]
        ]);
    }
}
</code></pre>

<p>My controller file is named <code>GoogleMap_MapController.php</code> to follow the naming scheme. I also have my ajax request, which needs to be written in vanilla javascript for this site:</p>

<pre><code>var xhr = new XMLHttpRequest();
            xhr.open('POST', '/actions/googleMap/getLocations');

            xhr.onreadystatechange = function() {
                if(xhr.status === 200) {
                    console.log(xhr);
                }
                else {
                    console.log("The request failed, returned status of " + xhr.status);
                    console.log(xhr);
                }
            };

            xhr.send(data);
</code></pre>

<p>I get a 404 error for page not found, which makes sense since it's not really a page, but the documentation says that it will attempt to determine if the request URL is an action first. I tried changing the ajax call to post to '/' instead, but that returns the HTML from the page '/' successfully, which is not what I'm trying to get at all. I've tried sticking my hidden input tags in a form tag, however the submit causes the page to redirect to this non-existent route. I added in <code>onsubmit="event.preventDefault();"</code> to my form tag, and while it doesn't redirect, I still get a 404 error response. I'm completely at a loss at this point any help is greatly appreciated.</p>

CC BY-SA 4.0
2019-03-01T01:19:41.017Z
Hi Frank, I'm gonna drop two shameless self-plugs here... (1) Check out how I've laid it out in the [Business Logic](https://github.com/doublesecretagency/craft-businesslogic/blob/master/businesslogic/controllers/BusinessLogicController.php#L67-L73) plugin template, and (2) I've got a plugin called [Smart Map](https://www.doublesecretagency.com/plugins/smart-map) which does a ton of stuff with Google Maps. Let me know if you have questions about either! :)
Lindsey D
CC BY-SA 4.0
2019-03-01T16:19:55.263Z
Cool thank you! I will check those out :)
WTFranklin
<plugin-development><ajax><craft2>
Ajax Post to Plugin Controller
# Ajax Post to Plugin Controller

(Asked by: username_0 on 2019-03-01)

I have been stuck on trying to make an ajax call to a custom plugin controller in Craft 2 for a few days after trying everything from following the documentation for Craft 2 as well as looking up other posts that basically reiterate what the documentation says, but nothing seems to work. According to the documentation for Craft 2, I should be able to use:
[PluginHandle]/[ControllerName]/[ActionName]

in a hidden field. I've even tried using the full method name, actionGetLocations, just to rule that out. Here is what I have:



Here is my controller:
requireAjaxRequest();

        return $this->returnJson([
            'success' => [
                'state' => "This would be states",
                'locations' => "This would be locations"
            ]
        ]);
    }
}

My controller file is named GoogleMap_MapController.php to follow the naming scheme. I also have my ajax request, which needs to be written in vanilla javascript for this site:
var xhr = new XMLHttpRequest();
            xhr.open('POST', '/actions/googleMap/getLocations');

            xhr.onreadystatechange = function() {
                if(xhr.status === 200) {
                    console.log(xhr);
                }
                else {
                    console.log("The request failed, returned status of " + xhr.status);
                    console.log(xhr);
                }
            };

            xhr.send(data);

I get a 404 error for page not found, which makes sense since it's not really a page, but the documentation says that it will attempt to determine if the request URL is an action first. I tried changing the ajax call to post to '/' instead, but that returns the HTML from the page '/' successfully, which is not what I'm trying to get at all. I've tried sticking my hidden input tags in a form tag, however the submit causes the page to redirect to this non-existent route. I added in onsubmit="event.preventDefault();" to my form tag, and while it doesn't redirect, I still get a 404 error response. I'm completely at a loss at this point any help is greatly appreciated.

**Tags:** plugin-development, ajax, craft2
> **Comments:**
> **username_1**: Hi Frank, I'm gonna drop two shameless self-plugs here... (1) Check out how I've laid it out in the [Business Logic](https://github.com/doublesecretagency/craft-businesslogic/blob/master/businesslogic/controllers/BusinessLogicController.php#L67-L73) plugin template, and (2) I've got a plugin called [Smart Map](https://www.doublesecretagency.com/plugins/smart-map) which does a ton of stuff with Google Maps. Let me know if you have questions about either! :)  
> **username_0**: Cool thank you! I will check those out :)  

---

## 1 answers

### Answer by username_1 on 2019-03-01. Score: 3 | Accepted answer

Based on your controller's class name, I believe you need to POST to this endpoint...
/actions/googleMap/map/getLocations

> **Comments:**
> **username_0**: Hey Lindsey, thank you for the answer. I tried switching to that, but unfortunately I still get the 404 error.  
> **username_0**: I take that back, I actually get a 400 Bad Request error now.  
> **username_1**: 400 is better than 404, it means the route is correct! It sounds like you're now bumping into [this CSRF issue...](https://craftcms.stackexchange.com/a/6588/45)  
> **username_0**: Hey you're right, I don't want to go far off topic from this post, so I made another since I'm still having the CSRF issue after checking out your link. This is the link to the post https://craftcms.stackexchange.com/questions/29697/csrf-token-is-null-or-empty-in-craft-2 if you have a minute to check it out :)  
> **username_1**: Good thinking. We'll continue on the new thread.  

---

