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.

General Questions

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

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

SS3 AJAX problem calling controller methox


Reply

2 Posts   486 Views

Avatar
msawebdev

1 November 2013 at 6:56pm Community Member, 15 Posts

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

3 November 2013 at 2:46am Community Member, 395 Posts

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

?