Skip to content
Home » Company » Sample applications and example code » VDF Ajax JSON service

VDF Ajax JSON service

The VDF Ajax JSON web service is built into the same WebApp as the VDF Ajax web service in WebApp.src:

   Object oWebApp is a cWebApp      Use SessionManager.wo      Use WebService.wo  // Original web service      Use Customer.wo      Use Order.wo      Use Invt.wo      Use Salesperson.wo      Use CustomerReport.wo      Use OrderDtlReport.wo      Use OrderReport.wo      Use Vendor.wo      Use CustomerTree.wo      Use JsonService.wo  // Our additional service   End_Object  // oWebApp

The JSON service only provides a sub-set of the Ajax functions, since several of the Ajax web service’s operations can be called without the need for complex JSON data. Those needing to be added are:

  • MetaDataProperties
  • Request
  • ValidateField
  • RemoteMethodInvocation
  • CreateSession

CreateSession is appended to the list because we don’t want to use ASP – just HTML – but the creation of a session is triggered from within ASP in the sample application, so we need to supply a web-service-based mechanism for this in addition to those operations which require complex input data.

Within the VDF Ajax JSON service, each function only consists of a few elements.

  1. The function signature is altered from the Ajax service in that it takes just a single String argument: “JSON”, although it returns the same data-type as its counterpart.
  2. The new command “StructNum” is used to determine the element representing the input data-type within the global array of Struct definitions.
  3. The “ParseJSONString” method of the service’s JSON reader object is invoked, passing it the input JSON string (by reference), a “ValueTree” type variable (also ByRef) and the Struct number.
  4. If the parsing succeeded, the new command “MoveValueTreeToStruct” (which is actually just an alias for the existing web service client command “ValueTreeDeserializeParameter“, but more understandably named for this context) is used to populate a variable of the type required by the Ajax web service.
  5. The original Ajax service function is invoked, passing it the data-type it expects and getting its response into a variable of its output type.
  6. That returned-to variable is itself then returned by the JSON service function, and, since the operation will have been invoked using the “/JSON” form of its URL, will be converted into JSON data for transmission back to the invoking client.

Here is an example – the JSON implementation of the “Request” operation:

   Function Request String JSON Returns TAjaxResponseData      TAjaxRequestData  req      TAjaxResponseData resp      tValueTree Values      Integer iStructNo      Boolean bOK         // Get the data-type index:      StructNum TAjaxRequestData to iStructNo         // This will deliver a Value-Tree with the JSON data      Get ParseJSONString of oReader (&JSON) (&Values) iStructNo to bOK         If bOK Begin         // Move the valueTree to the struct variable:         MoveValueTreeToStruct Values to req         // Forward the request to the original web service:         Move (Request(oWebService(Self), req)) to resp      End      Function_Return resp   End_Function  // Request

Essentially then, the JSON service functions just constitute “wrappers” for the Ajax service’s functions, which still do the computational heavy lifting. The passed JSON string is parsed into the required data type, then passed on, with the results being passed back to the calling JavaScript.