Problem with POST field routes

In a panel field I can post data with a route with $_GET but can’t get it to work with $_POST.

Get JS that work

$.fn.ajax = function(fieldname, data) {
  var blueprint_key = $('[data-field="' + fieldname + '"]').find('[name]').attr('name');
  var base_url = window.location.href.replace('/edit', '/field') + '/' + blueprint_key + '/' + fieldname + '/ajax/';

  var selector = '.field-name-' + blueprint_key;
  $.ajax({
    url: base_url + 'test/test',
    type: 'GET',
    success: function(result) {
      var obj = $(selector + ' iframe').attr('srcdoc', result + Date.now());
    }
  });
};

Post JS that does not work

$.fn.ajax = function(fieldname, data) {
  var blueprint_key = $('[data-field="' + fieldname + '"]').find('[name]').attr('name');
  var base_url = window.location.href.replace('/edit', '/field') + '/' + blueprint_key + '/' + fieldname + '/ajax/';

  var selector = '.field-name-' + blueprint_key;
  $.ajax({
  url: base_url,
  type: 'POST',
  data: data,
  success: function(result) {
      var obj = $(selector + ' iframe').attr('srcdoc', result + Date.now());
    }
  });
};

Get php route that work

public function routes() {
    return array(
      array(
        'pattern' => 'ajax/(:any)/(:any)',
        'method'  => 'get',
        'action' => function($var1, $var2) {

          $html = '';
          $html .= tpl::load( __DIR__ . DS . 'template-site.php', $data = array(
            'field' => $this,
            'page' => $this->page()
          ));

          return $html;
        }
      )
    );
  }

Post php route that does not work

public function routes() {
    return array(
      array(
        'pattern' => 'ajax',
        'method'  => 'POST',
        'action' => function() {

          $html = '';
          $html .= tpl::load( __DIR__ . DS . 'template-site.php', $data = array(
            'field' => $this,
            'page' => $this->page()
          ));

          return $html;
        }
      )
    );
  }

Fatal error: Call to a member function direction() on a non-object in C:\wamp\www\splitfield\panel\app\src\panel.php on line 362

I don’t know if my PHP is wrong or if it’s the JS.

UPDATE

Here is the plugin:

In blueprint:

fields:
  test:
    type: splitfield

It does not work, so it’s for testing purposes only.

Update 2

I can set this in the route:

 'method'  => 'GET|POST',

And this in my ajax template:

print_r($_POST);

But then again I get the same error as before:

Fatal error: Call to a member function direction() on a non-object in C:\wamp\www\splitfield\panel\app\src\panel.php on line 362

Update 3

In the route, I sent less stuff, it’s shorter:

$html .= tpl::load( __DIR__ . DS . 'template-site.php', $data = array());

I also found in the forum that $_POST is not used, so I used this instead:

<?php
$form_data = kirby()->request()->data();
print_r($form_data);
?>

Now I get a new error:

Array ( [csrf] => pZmUuMRoFkseW6PLQ8OvnM8C41NH2ei1 )

I change my approach an are now using a normal route instead of a panel field route. For the field I’m building it works just fine (only logged in users have access to it).

Would still be interesting to know why I got:

Array ( [csrf] => pZmUuMRoFkseW6PLQ8OvnM8C41NH2ei1 )

I don’t set solved, because it’s not. I just choosed another way.

That’s not an error, that’s a CSRF token that is used by the Panel for security reasons.

Yes but why. It’s not allowed to send data with post to a panel field route?

It is. What I meant is that the csrf token is not an error but instead tells you that your request was successful.

I can’t dig through your plugin’s JS and PHP code to find out what exactly is the reason why the data didn’t get passed though. I hope you understand.

It’s ok. If I really need a panel route in the future I’ll try to isolate the problem better.