SKG
SKG

Reputation: 510

JSON data From phpFox ajax call?

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.

Upvotes: 1

Views: 1814

Answers (2)

Purefan
Purefan

Reputation: 1506

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

Upvotes: 0

Transistor Bass
Transistor Bass

Reputation: 91

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.

Upvotes: 2

Related Questions