1

How to return JSON data in phpFox, ajaxCall?
In phpFox i am using $.ajaxCall('samplemodule.function' 'data=test');

How to return JSON data? and how to process on that data inside any js function.

JMax
  • 26,109
  • 12
  • 69
  • 88
SKG
  • 510
  • 4
  • 20

2 Answers2

2

In the file /module/samplemodule/component/ajax/ajax.class.php, create a function named function (per your example).

Inside that function, use this to return data back to the JS you're making your ajax call in:

$this->call('var myJSONObject=' . json_encode('Your Data Here'));

Or send something more interesting, instead of data=test, lets do userId= (their user ID) like this:

$iUserId = Phpfox::getLib('request')->getInt('userId');
$aUser = $aUser = Phpfox::getService('user')->getUser($iUserId);
$this->call('var aUser =' . json_encode($aUser));

Now you have aUser set up as a JSON object with the user's info loaded in to it.

  • This returns 'ReferenceError: Can't find variable: aUser' – Goddard Mar 07 '13 at 20:58
  • in the ajax.class.php you dont need to getLib('request'), you can just $this->get('userId') provided that you made the ajax call with $.ajaxCall('blah.blah', 'userId=321'); – Purefan Mar 11 '13 at 13:00
0

I think the problem is that you are confused as to how an ajax call works. In an ajax call your JS code will send a request to the server and continue executing the remaining javascript code, regardless of what happens in the server. So what you do is to return code from the ajax call:

JS Code -> Ajax Call -> Process in server -> JS Code

In that logic aboce, the last JS Code would call a javascript function with info taken from the "Process in server" stage, you can call a function and pass params to that functions, these params may be JSON objects if you wish.

I made a sample of how to do this in phpfox (ajax call + call JS function with JSON param) here, hope it helps

Purefan
  • 1,498
  • 24
  • 44