Posts tagged with “json”
Prototype Observer Pattern for using JSON via AJAX
This bit of Javascript code observes the “billing_info_bill_state” select field for changes every ’1.0’ seconds. Once changed, it sends the parameters to “update_totals.php”. It receives a JSON array via the X-JSON header and automatically eval’s it into an object that we can use to update the page.
<br />
new Form.Element.Observer(‘billing_info_bill_state’, 1.0, function updateTotals(e){
new Ajax.Request(‘update_totals.php’,{
parameters: {
method: ‘get’,
evalJSON: true,
state: e.value},
onSuccess: function(transport,json) {
$(‘billing_sales_tax’).update(json.tax);
$(‘billing_subtotal’).update(json.total);
}
});<br />
});<br />