jQuery x Prototype Ajax Get JSON

Just wanted to show how the same task can be performed at both jQuery (first snip) and Prototype.

The ideia here is to retrive an Array to the variable materials_id.


       jQuery.getJSON("/faturamento/ordem_compra/lista_pedidos_json/"+id, {},
        function(data){
          materials_id = data
        });
	
	new Ajax.Request('/faturamento/ordem_compra/lista_pedidos_json/'+id,{
		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(data){
			materials_id = data.responseText.evalJSON(true);
		}
	})

Beware, the response is not synchronous by default. That means that if the next line were alert(materials_id) You would likely see ‘undefined’.

On Prototype is pretty easy to set synchronous, but in jQuery, a little more troublesome. Anyway, you can make it yourself, just set after the call and before you need the materials_id a while with the condition, that checks the typeof materials_id 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s