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

Ajax 404 problem


Go to End


3 Posts   2017 Views

Avatar
moniuch

Community Member, 11 Posts

1 May 2014 at 8:05pm

Edited: 01/05/2014 9:16pm

I'm trying to get first things first in Ajax in SS 3.1 with this rudimentary example of an Ajax calculator:
JS:

            $('button').on('click', function(){
                $.get('myajax/add/5/6', function(data){
                    alert(data.result);
                })
            })

AjaxPage.php:

<?php
class AjaxPage extends Page {

}

class AjaxPage_Controller extends Page_Controller {
    private static $url_handlers = array(
        'myajax/add//$num1/$num2' => 'add',
        'myajax/multiply//$num1/$num2' => 'multiply',
    );
    private static $allowed_actions = array (
        'add',
        'multiply'
    );

    public function add(){
        $v1 = (int) $this->getRequest()->param('num1');
        $v2 = (int) $this->getRequest()->param('num2');
        echo json_encode(array('result' => $v1 + $v2));
    }

    public function multiply(){
        $v1 = (int) $this->getRequest()->param('num1');
        $v2 = (int) $this->getRequest()->param('num2');
        echo json_encode(array('result' => $v1 * $v2));
    }
}

debug info
Debug (line 250 of RequestHandler.php): Testing 'myajax/add//$num1/$num2' with '' on AjaxPage_Controller
Debug (line 250 of RequestHandler.php): Testing 'myajax/multiply//$num1/$num2' with '' on AjaxPage_Controller
Debug (line 250 of RequestHandler.php): Testing '$Action//$ID/$OtherID' with '' on AjaxPage_Controller
Debug (line 258 of RequestHandler.php): Rule '$Action//$ID/$OtherID' matched to action 'handleAction' on AjaxPage_Controller. Latest request params: array ( 'Action' => NULL, 'ID' => NULL, 'OtherID' => NULL, )
Debug (line 184 of RequestHandler.php): Action not set; using default action method name 'index'

routes.yml
Director:
rules:
'myajax/add//$num1/$num2': 'AjaxPage_Controller'
'myajax/multiply//$num1/$num2': 'AjaxPage_Controller'

So, could anyone shed some light on me as per why the first two attempts keep failing and the default fallback comes into action?

Avatar
moniuch

Community Member, 11 Posts

7 May 2014 at 7:55pm

Edited: 07/05/2014 7:55pm

Now I know - the clue was the understanding and the usage of the shift point.

In routes.yml I should've had:

myajax//add/$num1/$num2

whereas, in the controller's url_handlers:

'add//$num1/$num2' => 'add'

HTH someone.

Avatar
snaip

Community Member, 181 Posts

24 September 2015 at 5:43am

Edited: 24/09/2015 5:46am

Hi

I have a problem. Ajax POST is returning 404

This is my controller

class Rezerwacja_Controller extends ApplicationPage_Controller {
   private static $url_handlers = array(
        'getCode' => 'getCode'
    );

   private static $allowed_actions = array('getCode');

    function getCode() {
    echo '1';    
    }
}

and AJAX

	$('#Form_RegisterForm_kod_promocyjny').on('input', function() {
		var r = $.ajax({
			method: 'POST',
			url: 'rezerwacja/getCode'
		});
		r.done(function(data) {
			console.log(data);
		});
		r.fail(function(data) {
			console.log(data);
		});
	});

The request is going to http://localhost/mysite/rezerwacja/getCode