With Craft Commerce, how can I call getAllStates from my own plugin via ajax? I am trying to call $states = craft()-&gt;commerce_states-&gt;getAllStates(); from within my own plugin, via an ajax request.

MyPlugin.php

if (craft()-&gt;request-&gt;isAjaxRequest()) {
    return $this-&gt;returnJson(['states' =&gt; craft()-&gt;commerce_states-&gt;getAllStates()]);
}


The above will return this:

Object {states: Array[72]}


However, as I'm walking myself through it, I can see that the original call:

var states = {{ craft.commerce.statesArray|json_encode|raw }};


Gives me these objects:

Object {13: Object, 38: Object, 233: Object}
Object {13: Object, 38: Object, 233: Object}


So it's pretty obvious i'm not calling the correct method. I am using requirejs within my project and having the javascript right in the template is returning errors - it can't access jquery. 

Thank you for any suggestions!
Instead of calling getAllStates() to get the same data back as the twig call:

var states = {{ craft.commerce.statesArray|json_encode|raw }};


Within your plugin you need to call getStatesGroupedByCountries()

MyPlugin.php

if (craft()-&gt;request-&gt;isAjaxRequest()) {
    return $this-&gt;returnJson(['states' =&gt; craft()-&gt;commerce_states-&gt;getStatesGroupedByCountries()]);
}