Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SS3 AJAX problem calling controller methox


Go to End


2 Posts   2115 Views

Avatar
msawebdev

Community Member, 15 Posts

1 November 2013 at 6:56pm

I am refactoring an application from SS2.4.n to SS3.1 and am having trouble with the ajax method not finding the controller method (returning 404 error)

in the controller I have:
public static $allowed_actions = array (
'getStoreList'
);

public function getStoreList($data){
$array = array();

if($this->isAjax) {
$dealerID = (int) $data->postVar('dealerID');

Session::set('DealerID', $dealerID);

if($stores = DataObject::get('Store', 'DealerGroupID=' . $dealerID . ' AND JSSRegionID= '. $this->getCurrentRegionID() ) ){

foreach($stores as $mem) {
$array[] = array("Data" => $mem->ID,
"Label" => $mem->Name
);
}
}

}

return json_encode($array);
}

in the javascript/jQuery I have:

$storelistURL = $this->Link().'getStoreList';

Requirements::customScript("
(function($) {
$(document).ready(function() {

.
.
.
.
$.ajax({ url: '{$storelistURL}',
data: {dealerID:$(this).val()},
type:'POST',
dataType: 'json',
success: function( data ) {
var arrayLen = data.length;
$('option', select).remove();

if(arrayLen){
select.append($('<option>', {value: '', text: 'Select Store..'}));
for(var i=0; i< arrayLen; i++){
var item = data;
select.append($('<option>', {value: item.Data, text: item.Label}));
}
}else{
select.append($('<option>', {value: '', text: 'No stores returned..'}));
}
}
});
.
.
.

Works on the old app but now getting a 404 error
POST http://localhost/product_knowledge/dealer-registration/StoreList 404 (Not Found) jquery.min.js:2
send jquery.min.js:2
v.extend.ajax jquery.min.js:2
(anonymous function) (index):235
v.event.dispatch jquery.min.js:2
o.handle.u

Do I need to set up routes in the yml file?

Have I missed something really basic?

Any and all help would be greatly appreciated (while I still have hair to pull out)

Avatar
Nivanka

Community Member, 400 Posts

3 November 2013 at 2:46am

i am not sure whether this is the exact problem, but in your URL it looks like it is not directed to the controller action.

your HTTP requests says the URL is " http://localhost/product_knowledge/dealer-registration/StoreList

shouldn't it be http://localhost/product_knowledge/dealer-registration/getStoreList

?