ajaxJson

This function provides an easy way to call an HTTP url that takes a JSON input and returns a JSON response. The method automatically parses a parameter object to JSON and result JSON into a result object or value.

You can pass a value or object that is turned into JSON which by default is POST'ed to the server. If no parameter is provide the request defaults to a GET operation. You can override the verb via the options.verb parameter. You can also use the options.noPostEncoding option to prevent JSON encoding of parameters.

The result is handled either via the two callback parameters for success and error respectively, or via a returned jQuery Promise object.

To call a service with a Promise:

JSON Object or value (just pass a value):

function CallServiceWithPromise()
{
    ajaxJson("services/timetrakkerservice.svc/LoadCustomer",
			{Pk:12,Entered:new Date()},{verb: PUT})
		.then(function (result)  { // parsed object instance
		    alert( result.d.Company);
		    alert( result.d.ProjectEntities[1].CustomerEntity.LastName);
		},onError);

function onError(error)  // error object
{
    alert("Error: " + error.message); // parsed error
}

Or call with callback methods:

Raw POST string:

function CallServiceWithCallbacks()
{
    ajaxJson("services/timetrakkerservice.svc/SaveCustomer",
			"Name=Rick&Company=West+Wind",
			Callback,onError,
			{ contentType: "application/x-www-urlencoded", noPostEncoding: true});
}
function Callback(result)
{
    alert( result.d.Company);
    alert( result.d.ProjectEntities[1].CustomerEntity.LastName);
}
function onError(error)
{
    alert("Error: " + error.message);
}

Alternately, ajaxJson() c

Note that if passing a string you should pass the content type explicitly - otherwise the string will be posted as JSON.

If you are calling a CallbackHandler or AjaxMethodCallback control on the server use the ajaxCallMethod function instead.

ajaxJson(url,value,callback,errorCallback,options)

Return Value

jQuery Promise Object

Parameters

url
The URL to access including any query string data.

value
a single value or array that is passed to the server. For MS Ajax pass an object with a property for each parameter. For Web Connection pass an array of values one for each parameter.

If this value is null a GET request is sent. If options don't override behavior a POST request is sent when data is provided.

callback
Optional function that is called on a successful callback. Function receives raw HTTP result string as a parameter.

errorCallback
Optional function that is called when an error occurs. Receives a CallbackException object as a parameter that holds exception information.

options
An optional object map that configures the HTTP operation based on settings of the HttpClient class. If the callBack and errorCallback functions are not provided options can be passed as the 3rd parameter.

Values:

  • accepts - accept header content type
  • contentType - request content type
  • noPostEncoding - don't turn parameter to JSON
  • evalResult - don't turn result to JSON
  • isMsAjaxJson - for MS AJAX style calls
  • method - HTTP Verb (POST or GET are defaults)
  • timeout - request timeout

See also:

Using CallbackHandler with REST Calls | ajaxCallMethod

© West Wind Technologies, 1996-2018 • Updated: 10/09/17
Comment or report problem with topic