Jump to:

22993 Posts in 11829 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » SS3 AJAX problem calling controller methox

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

Page: 1
Go to End
Author Topic: 298 Views
  • msawebdev
    Avatar
    Community Member
    10 Posts

    SS3 AJAX problem calling controller methox Link to this post

    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)

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: SS3 AJAX problem calling controller methox Link to this post

    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

    ?

    298 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.