Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,090 of 9,090 pages
About the help in other languages
All help pages

JSON endpoints with ezjscore

Give your pages' JavaScript a JSON back end with ezjscore server functions - the [ezjscServer_<name>] block, the static method, the call URL and answer envelope, permission checks, form tokens, and when to use…

Download as PDF

Address: https://doc.exponential.earth/exphelp/viewer/help/creators/app-creators/json-endpoints-with-ezjscore.md

On this page

JSON endpoints with ezjscore

This page is for application creators whose pages need data from PHP without a page reload: autocomplete, live counters, small saves. ezjscore calls registered PHP functions over HTTP and answers in JSON (ezjscore, Backend ezjscore services).

Contents

  1. How a call works
  2. Registering a server function
  3. The class
  4. Calling it from JavaScript
  5. Security
  6. ezjscore, expservices or REST?

How a call works

<root>/ezjscore/call/<name>::<function>[::<arg>...]?ContentType=json

The router (ezjscServerRouter) refuses any name without a block in ezjscore.ini, calls <class>::<function>( array $args ) and wraps the return value: {"error_text": "", "content": <your value>}. A test function ships: <root>/ezjscore/call/ezjsc::time.

Registering a server function

# extension/expdemo/settings/ezjscore.ini.append.php
<?php /* #?ini charset="utf-8"?
[ezjscServer_expdemo]
Class=expDemoServerFunctions
*/ ?>

Optional keys (see the comments of [ezjscServer] in extension/ezjscore/settings/ezjscore.ini): File, TemplateFunction, Functions[] (policy functions of [ezjscServer] FunctionList[] the user needs).

The class

<?php
class expDemoServerFunctions
{
    public static function count( array $args )
    {
        $user = eZUser::currentUser();
        $access = $user->hasAccessTo( 'expdemo', 'read' );
        if ( $access['accessWord'] === 'no' )
            throw new Exception( 'Access denied' );
        $userID = isset( $args[0] ) ? (int)$args[0] : 0;
        return array( 'count' => eZPersistentObject::count( expDemoItem::definition(), array( 'user_id' => $userID ) ) );
    }
}

ezjscore/call does not check a policy by itself: every function checks for itself.

Calling it from JavaScript

// extension/expdemo/design/standard/javascript/expdemo.js
fetch( root + 'ezjscore/call/expdemo::count::' + encodeURIComponent( userId ) + '?ContentType=json',
       { credentials: 'same-origin', headers: { 'Accept': 'application/json' } } )
    .then( function ( r ) { return r.json(); } )
    .then( function ( data ) { if ( !data.error_text ) show( data.content.count ); } );

root comes from a JSON data block in the pagelayout, not an inline script (Content Security Policy for your code). Writes are POSTs and carry the form token.

Security

  • Check permissions in every function; validate and cast every argument.
  • Return data, not HTML; if you must return HTML, escape it.
  • Writes only by POST, with the form token.
  • Do not expose more than the page needs.

ezjscore, expservices or REST?

ForUse
your own pages' JavaScriptezjscore server functions
admin features from scripts, apps or other front endsexpservices, with discovery, one envelope and API tokens (Remote services)
content for other programs, OAuth for third partiesREST (REST API)

Become an Exponential Partner

Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x