Accessing PTV xServer internet with the JSON API

  • One way to access xServer internet from a JavaScript application is the JSON API in combination with jQuery. jQuery is a JavaScript library which brings AJAX features. With these features it is possible to send browser-independent, asynchronous requests. The sample describes a xLocate request:

// enter your token here!
var token = '<INSERT-YOUR-TOKEN-HERE>';

// url we're sending the request to
var url = 'https://xlocate-eu-n-test.cloud.ptvgroup.com/xlocate/rs/XLocate/findAddress';

 // sample PTV xLocate request
 var request = {
  "addr": {
     "country": "L",
     "postCode": "",
     "city": "Wellenstein",
     "city2": "",
     "street": "Rue de la Moselle 5",
     "houseNumber": ""
  },
  "options": [],
  "sorting": [],
  "additionalFields": [],
  "callerContext": {
     "properties": [{
      "key": "CoordFormat",
      "value": "PTV_MERCATOR"
     }, {
      "key": "Profile",
      "value": "default"
     }]
  }
 };
  • A helper method runRequest executes a JSON request on PTV xServer internet, given the URL endpoint, the token and the callbacks to be called upon completion. The error callback is parameterless, the success callback is called with the object returned by the server.

var runRequest = function(url, request, token, handleSuccess, handleError) {
   $.ajax({
      url: url,
      type: "POST",
      data: JSON.stringify(request),
       
      headers: {
         "Authorization": "Basic " + btoa("xtok:" + token),
         "Content-Type":"application/json"
      },
 
      success: function(data, status, xhr) { 
         handleSuccess(data); 
      },
 
      error: function(xhr, status, error) { 
         handleError(xhr); 
      }
   });
};