Post by chrisg42 » Wed Dec 07, 2011 8:50 am

Hi,

I've been trying to get an ajax function working in Opencart Admin and instead of getting the results from the class method as specified in the ajax url I get either the admin index page or the permission denied page returned. It's as though the class function isn't being called or the token is not being validated correctly.

I've tried including the token in data as well as the url. I've also tried using post and get types.

This is the code that doesn't work but I can't see why

On the client side...

Code: Select all

$.ajax({
		type: 'post',
		url: 'index.php?route=common/coloradjust/saveChanges?token=<?php echo $token; ?>',
		data: format_data,
		dataType: 'text',
		success: function(text) {
			$('#messages').html(text);
		}
	});

on the server side...

Code: Select all

class ControllerCommonColorAdjust extends Controller {
...

	public function saveChanges()
	{
		echo "This has come back from the function call";
	}

}

To get around this for now I've created a hack that works outside the Opencart design and it works..

Client-side (the token is included in the format_data)

Code: Select all

$.ajax({
		type: 'post',
		url: 'ajax/color_adjust.php',
		data: format_data,
		dataType: 'text',
		success: function(text) {
			$('#messages').html(text);
			//parent.$('#dialog').remove();
		}
	});
server side...

Code: Select all

<?php
session_start();

// check session token
if ($_REQUEST['token'] != $_SESSION['token'])
{
	echo "Invalid Token";
	exit;
}
	

echo "This is the returned results from color_adjust.php<br>";
print_r($_POST);
?>
I would really like to be able to do this the 'proper' way.
Any help would be appreciated

thanks.
Last edited by chrisg42 on Mon Dec 12, 2011 12:26 pm, edited 1 time in total.

User avatar
Newbie

Posts

Joined
Sun Oct 30, 2011 2:06 pm


Post by qahar » Wed Dec 07, 2011 7:11 pm

You can try code bellow, usually I use something like this:

Code: Select all

public function saveChanges() {
     $json['success'] = 'This is the returned results from color_adjust.php';
     $this->response->setOutput(json_encode($json));
} 

Code: Select all

$.ajax({
      type: 'post',
      url: 'index.php?route=common/coloradjust/saveChanges&token=<?php echo $token; ?>',
      data: format_data,
      dataType: 'json',
      success: function(json) {
            if (json['success']) {
                  $('#messages').html(json['success']);
            }
      }
 });

User avatar
Expert Member

Posts

Joined
Tue Jun 29, 2010 10:24 pm
Location - Indonesia

Post by chrisg42 » Mon Dec 12, 2011 12:24 pm

Thanks qahar that did the trick. I did originally try using json but I must have mucked something up in the process.

User avatar
Newbie

Posts

Joined
Sun Oct 30, 2011 2:06 pm

Who is online

Users browsing this forum: No registered users and 10 guests