ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage functions / Web-specific functions / Miscellaneous WEBDEV functions
  • Procedure for processing JSON data
  • Page that returns JSON data
  • IMPORTANT: JSON validity
  • Security
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
JSONExecuteExternal (Function)
In french: JSONExécuteExterne
Warning
From version 22, It is recommended to use JSONExecute. Indeed, the web services returning data in JSON format are now using the CORS protocol to simplify the security.
Calls an external server URL that returns data in JSON format (JavaScript Object Notation). The server data is evaluated and transmitted to the callback function in object format.
This function is not locking. The other processes continue to run (no matter whether the data is retrieved or not).
Once the result is available, JSONExecuteExternal automatically calls a browser procedure to retrieve the result.
Example
// Exécution d'un service JSON sur google
// Google attend le nom de la procédure qui va traiter l'objet JSON 
// dans le paramètre qui a pour nom "callback"
// et ici la procédure qui va traiter l'objet s'appelle "AgendaGoogle"
JSONExecuteExternal(...
	"http://www.google.com/calendar/feeds/monsite@gmail.com" + ...
	"/public/full?alt=json-in-script&orderby=starttime&max-results=15&" + ...
	"singleevents=true&sortorder=ascending&futureevents=true", ...
	 "callback", AgendaGoogle)

// Cela génerera une URL d'appel à la fonction json de ce type:
// http://www.google.com/calendar/feeds/monsite@gmail.com/public/full?alt
// =json-in-script&orderby=starttime&max-results=15&singleevents=true&
// sortorder=ascending&futureevents=true&callback=AgendaGoogle
// Exécution d'un service JSON sur une page AWP
JSONExecuteExternal(...
	"http://monsite/monsite_WEB/FR/PAGE_Objet.awp?id=12", ...
	"JsonCallback", ReponseFonction)
// La page AWP attend donc un paramètre qui se nomme "JsonCallback" 
// qui contiendra le nom de la procédure a exécutée en retour
Syntax
JSONExecuteExternal(<Page URL> , <Parameter name> , <Procedure name>)
<Page URL>: Character string
URL of the page that returns the JSON data.
<Parameter name>: Character string
Name of the parameter added to the URL by WEBDEV to indicate the name of the procedure for processing the JSON data to the relevant server. The name of this parameter depends on the documentation of the JSON function called, it is "callback" in most cases.
<Procedure name>: Character string
Name of the browser procedure (global or local procedure) that performs the process of the JSON data.
This procedure has the following format:
PROCEDURE <Procédure navigateur> (<Résultat JSON de l'appel>)
The parameter passed to this procedure is a dynamic object that contains the JSON data. It must be indicated in the declaration of the procedure.
Remarks

Procedure for processing JSON data

The JSON result must be returned as a JSON code enclosed in a call to the procedure used to process this data.
The name of this procedure is directly passed as a parameter in the URL for calling the page. The name of this parameter is specified in JSONExecuteExternal.
For example:
JSONExecuteExternal("http://MonSite/MonSite_WEB/FR/PAGE_Objet.awp?id=12", ...
	"JsonCallback", ReponseFonction)
will be used to build the following URL:
"http://MonSite/MonSite_WEB/FR/PAGE_Objet.awp?id=12&JsonCallback=ReponseFonction"
// WEBDEV a ajouté: &JsonCallback=ReponseFonction en plus 
// à la fin de l'URL pour indiquer la fonction de retour

Page that returns JSON data

The page that returns the data in JSON format can be an Active WEBDEV Page, a PHP page or any other type of page.
If it is a WEBDEV page, it can be an Active WEBDEV Page or a PHP page. This page must return the JSON data with StringDisplay. Furthermore, StringToUTF8 must also be used to get a valid format.
For example, the following code creates the string that will be returned. This string contains the code used to define an object and an array. The code used is JavaScript code.
PageParameter is used to process an external call. In this case, the JSON data returned is formatted with the name of the process procedure.
// Chaîne contenant un objet JSON
sObjet is string = [
	%1 ( {id: 12,
	liste:  [
		{nom: "smith", prénom: "john"},
		{nom: "dupond", prénom: "marie"},
		{nom: "martin", prénom: "lauré"}]
} );
]

// Ajout du nom de la procédure callback (à la place de %1)
// Ce nom est attendu dans un paramètre qui a pour nom "JsonCallback"
sObjet = StringBuild(sObjet, PageParameter("JsonCallback"))

// Renvoie la chaîne de l'objet JSON 
// avec le nom de la procédure javascript à appeler
StringDisplay(StringToUTF8(sObjet))

IMPORTANT: JSON validity

The validity of the JSON data received from the server is not checked. Make sure that the called page is a trusted page to prevent JavaScript injection attacks in the current page.

Security

To protect data, you can use a secure page (https URL).
Component: WDJS.DLL
Minimum version required
  • Version 14
Comments
Click [Add] to post a comment

Last update: 09/21/2024

Send a report | Local help