Jump to content

[SOLVED] Ajax call from module


PhpMadman

Recommended Posts

Hello i'm working on a module that has a fron controller that makes and ajax call.

The ajax works, but the return of the jsonData does not.

This is my JS file.

$(document).ready(function(){
	var query = $.ajax({
		type: 'POST',
		url: baseDir + 'modules/pim/pim-ajax.php',
		data: 'method=myMethod&data=x',
		dataType: 'json',
		success: function(jsonData) {
			console.log(jsonData); // this is null!?
			alert('Wee');
		}
	});
});

And this is the ajax file.

<?php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
switch (Tools::getValue('method')) {
  case 'myMethod' :
    die(Tools::jsonEncode( array('result'=>'my_value'));
    break;
  default:
    exit;
}

When refreshing the page. I get the Wee alert, but the console says the jsonData is null.

I'm using 1.6.0.6.

 

What am I missing here?

 

Edited by PhpMadman (see edit history)
Link to comment
Share on other sites

add:

print_r($_POST);

before:

switch (Tools::getValue('method')) {
  case 'myMethod' :
    die(Tools::jsonEncode( array('result'=>'my_value'));
    break;
  default:
    exit;
} 

turn on browser console ctrl+shift+j in chrome, then try to call this ajax query.

what you see in console output RETURN value for this ajax query?

Link to comment
Share on other sites

Hello.

I don't have chrome. I use Chronimum and Firefox.

Chronium output from console of pim-ajax.php

Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,sv;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:22
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:baf41ec989746b918a1073a072047ca2=NutO0FbhBk0t4zs0PyrYS2Z4cF7TlhHb1FXuPlD2cbpA1n8dNuOpofpBwSjXixIn13eaP2FmX1BEgTsdR62cPiJkt%2FTMFtuHy8cQ1hzIkWDxfzwdCooC9oamhyoIAJ1YAgsjYwaBQiR6ZK0O4E94kDeffJR7q1LSi[spam-filter]QQP7r9uMmFHlBxjjUaCvuw%2FUM2wTRnE2QxDimjqbK0Fj6t0J7U6TnfWVXP9j1mhFX11saU7bX[spam-filter]E0bk1ZnMdTxHPUiR6dP7RDpOriu9HMxLwPYhEpFSyEFRp91oefr5%2BKMxvvdz91Wsekz2GNtuPY2PhPyF000235; 50611aebd1c5abb315ba70ce0ff2bb16=1mxhaE7W%2Fxk5h2D%2FrPWb71LgfkMg312wJgfLeJMXJz%2Fk4q3EiwrkLHi1O6E421gqwxZLV%2FKxb2XfxmWornBJvJzySG4%2BnseUF8nZI0NXcV5YJgWLb8x%2BnwsQfIcqo7gb1J2A4VjN2SOmSmB4guK0Vw%3D%3D000107
Host:www.fairmobil.se
Origin:http://www.fairmobil.se
Referer:http://www.fairmobil.se/pim
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
method:myMethod
data:x
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Wed, 11 Jun 2014 13:04:43 GMT
Keep-Alive:timeout=5, max=93
Server:ODERLAND Apache
X-Powered-By:PHP/5.5.13

But I can't see that it's print any data from $_POST but it does recive it.

Test your self:  http://www.fairmobil.se/pim

Link to comment
Share on other sites

I did some more testing.

my ajax file now has this.

<?php
echo('before config');
require_once(dirname(__FILE__).'../../../config/config.inc.php');
echo('before init');
require_once(dirname(__FILE__).'../../../init.php');
echo('[-[');
print_r($_POST);
echo(']-]');
switch (Tools::getValue('method')) {
  case 'myMethod' :
    die(Tools::jsonEncode( array('result'=>'my_value'));
    break;
  default:
    exit;
}

Nothin gets pritend.

Link to comment
Share on other sites

It's working now!

<?php
include_once(dirname(__FILE__).'../../../config/config.inc.php');
include_once(dirname(__FILE__).'../../../init.php');
switch (Tools::getValue('method')) {
    case 'myMethod' :
        $return = array('result' => 'my really shitty value');
        echo json_encode($return);
        break;
    default:
        exit;
}
?>

So... My guess, it was the require_once that screwed with it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...