ajaxJson

This function is an easy way to call a URL and receive an evaluated JSON response. You can pass POST data to the server either as a parsed JSON object or as a raw POST buffer.

JSON Parameter (just pass a value):

function CallService()
{
    ajaxJson("services/timetrakkerservice.svc/LoadCustomer",
			{Pk:12,Entered:new Date()},			
			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);
}

Raw POST data:

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

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)

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.

<>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 passes in options:

contentType: the content type of the data to send. Default is application/json method: POST or GET. Default is POST noPostJsonEncoding: determines if post parameter is JSON encoded. Default is false.

If the options object is not supplied the value parameter is converted to JSON and POSTed to the server using application/json as the content type.

JSON encoding occurs only if noPostJsonEncoding is false, the method is POST and the contentType is application/json. All three of these settings are the default and changing any one of them will cause the value not to be JSON encoded and posted in whatever format you supply.


See also:

Using CallbackHandler with REST Calls | ajaxCallMethod

© West Wind Technologies, 1996-2016 • Updated: 12/12/15
Comment or report problem with topic