KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: The importance of the Type parameter of WEB SEND TEXT
PRODUCT: 4D | VERSION: 14.1 | PLATFORM: Mac & Win
Published On: June 12, 2014

With the growing use of JSON to communicate between 4D and web clients it is important to know the distinction in v14 between sending JSON to a web client using WEB SEND TEXT($JSON_T) and WEB SEND TEXT($JSON_T;"text/json").

By default, if the type parameter is omitted, 4D v14 assumes that the data sent is of the "text/html" type, and adds that mime type to the HTTP header.

Consider the example JavaScript function shown below when the JSON is sent WEB SEND TEXT($JSON_T) , in v14 without the type parameter.

function submitPartnerFormData( OBJ ) {
  $.ajax({
    type: "POST",
    url:  OBJ.url,
    data: OBJ.params,
    success: function( data ) {
      if( data !== '' ) {
        var fields = JSON && JSON.parse( data ) || $.parseJSON( data );
        // Do something with the fields JSON object
      }
    }
  });
}


The browser delivers the parameter "data" as raw text, assuming it is html as the HTTP header specifies. To use the 4D v14 JSON it will have to be converted to a JSON object using JavaScript similar to that shown above.

If JSON is sent in v14 using the type parameter "text/json", WEB SEND TEXT($JSON_T;"text/json"), all modern web browsers will pass a JSON object as the function's "data" parameter, see the example JavaScript function below. This reduces the amount of JavaScript that has to be loaded and speeds the presentation of data to the user.

function submitPartnerFormData( OBJ ) {
  $.ajax({
    type: "POST",
    url:  OBJ.url,
    data: OBJ.params,
    success: function( data ) {
      if( data !== undefined ) {
        // Do something with the data JSON object
      }
    }
  });
}